# Install packages
if (!requireNamespace("patchwork", quietly = TRUE)) {
install.packages("patchwork")
}if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}if (!requireNamespace("cowplot", quietly = TRUE)) {
install.packages("cowplot")
}
# Load packages
library(patchwork)
library(ggplotify)
library(cowplot)
Pie Group
Note
Hiplot website
This page is the tutorial for source code version of the Hiplot Pie Group
plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
patchwork
;ggplotify
;cowplot
Data Preparation
# Load data
<- read.delim("files/Hiplot/139-pie-group-data.txt", header = T)
data
# Convert data structure
"genre"] <- factor(data[,"genre"], levels = unique(data[,"genre"]))
data[,"mpaa"] <- factor(data[,"mpaa"], levels = unique(data[,"mpaa"]))
data[,
# View data
head(data)
title year length budget rating
1 Shawshank Redemption, The 1994 142 25 9.1
2 Lord of the Rings: The Return of the King, The 2003 251 94 9.0
3 Lord of the Rings: The Fellowship of the Ring, The 2001 208 93 8.8
4 Lord of the Rings: The Two Towers, The 2002 223 94 8.8
5 Pulp Fiction 1994 168 8 8.8
6 Schindler's List 1993 195 25 8.8
votes mpaa genre
1 149494 R Drama
2 103631 PG-13 Action
3 157608 PG-13 Action
4 114797 PG-13 Action
5 132745 R Drama
6 97667 R Drama
Visualization
# Pie Group
<- c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF","#F39B7FFF","#8491B4FF",
col "#91D1C2FF","#DC0000FF","#7E6148FF","#B09C85FF")
<- list()
plist for (i in 1:length(unique(data[,"mpaa"]))) {
<- data[data[,"mpaa"] == unique(data[,"mpaa"])[i],]
data_tmp <- table(data_tmp[,"genre"])
x <- as.ggplot(function(){
ptmp par(oma=c(0,0,0,0))
pie(x,
labels = sprintf("%s\n(n=%s, %s%%)", names(x), x,
round(x / sum(x) * 100, 0)),
col = col,
main = paste0("mpaa", ":", unique(data[,"mpaa"])[i]),
edges = 200,
radius = 0.8,
clockwise = F
)
})<- ptmp
plist[[i]]
}
plot_grid(plotlist = plist, ncol = 2)
