GGPIE

Authors

[Editor] Hu Zheng;

[Contributors]

The pie chart is a statistical chart that shows the proportion of each part by dividing a circle into sections.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: ggpie; dplyr; ggplot2; cowplot

# Install packages
if (!requireNamespace("ggpie", quietly = TRUE)) {
  install.packages("ggpie")
}
if (!requireNamespace("dplyr", quietly = TRUE)) {
  install.packages("dplyr")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("cowplot", quietly = TRUE)) {
  install.packages("cowplot")
}

# Load packages
library(ggpie)
library(dplyr)
library(ggplot2)
library(cowplot)

Data Preparation

The loaded data are different groups and their data.

# Load data
data <- read.delim("files/Hiplot/068-ggpie-data.txt", header = T)

# Convert data structure
axis <- c("am", "cyl")
data[, axis[1]] <- factor(data[, axis[1]], levels = unique(data[, axis[1]]))
data[, axis[2]] <- factor(data[, axis[2]], levels = unique(data[, axis[2]]))

# View data
head(data)
   mpg cyl disp  hp drat    wt  qsec vs am gear carb
1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Visualization

# GGPIE
plist <- list()
for (j in unique(data[, axis[2]])) {
  plist[[j]] <- ggpie(
    data = data[data[, axis[2]] == j,],
    group_key = axis[1], count_type = "full",
    label_type = "horizon", label_size = 8,
    label_info = "all", label_pos = "out") + 
    scale_fill_manual(values = c("#00468BFF","#ED0000FF")) +
    ggtitle(j)
  }

plot_grid(plotlist = plist, ncol = 3)
FigureΒ 1: GGPIE

In a circle graph, the arc length of each slice (the arc length of its center Angle and the region corresponding to its center Angle) is proportional to the number represented. The pie chart shows the number of samples for the 1 to 4 components and the corresponding proportions. The number of samples in one group is 13, accounting for 11.71%; the number of samples in two groups is 34, accounting for 30.63%; the number of samples in three groups is 21, accounting for 18.92%; and the number of samples in four groups is 43, accounting for 38.74%.