Pie

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-01-17

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Pie plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:

https://hiplot.cn/basic/pie?lang=en

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: data.table; jsonlite; ggplot2; dplyr

# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
  install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
  install.packages("jsonlite")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("dplyr", quietly = TRUE)) {
  install.packages("dplyr")
}

# Load packages
library(data.table)
library(jsonlite)
library(ggplot2)
library(dplyr)
sessioninfo::session_info("attached")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.2 (2025-10-31)
 os       Ubuntu 24.04.3 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       UTC
 date     2026-01-17
 pandoc   3.1.3 @ /usr/bin/ (via rmarkdown)
 quarto   1.8.27 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package    * version date (UTC) lib source
 data.table * 1.18.0  2025-12-24 [1] RSPM
 dplyr      * 1.1.4   2023-11-17 [1] RSPM
 ggplot2    * 4.0.1   2025-11-14 [1] RSPM
 jsonlite   * 2.0.0   2025-03-27 [1] RSPM

 [1] /home/runner/work/_temp/Library
 [2] /opt/R/4.5.2/lib/R/site-library
 [3] /opt/R/4.5.2/lib/R/library
 * ── Packages attached to the search path.

──────────────────────────────────────────────────────────────────────────────

Data Preparation

The loaded data are different groups and their data.

# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/pie/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)


# Convert data structure
colnames(data) <- c("Group", "Value")
data <- data %>%
  arrange(desc(Group)) %>%
  mutate(prop = Value / sum(data$Value) * 100) %>%
  mutate(ypos = Value / length(unique(Group)) +
           c(0, cumsum(Value)[-length(Value)]) + 5)

# View data
head(data)
   Group Value     prop   ypos
1 Group4    43 38.73874  15.75
2 Group3    21 18.91892  53.25
3 Group2    34 30.63063  77.50
4 Group1    13 11.71171 106.25

Visualization

# Pie
p <- ggplot(data, aes(x = "", y = Value, fill = Group)) +
  geom_col(width = 1) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  geom_text(aes(y = ypos, 
                label = sprintf("%s\n(n=%s, %s%%)", Group, Value,
                                round(Value / sum(data$Value) * 100, 2))), 
            color = "white", fontface = "bold") +
  coord_polar(theta = "y", start = 0, direction = -1) +
  guides(fill = guide_legend(title = "Group")) +
  scale_fill_discrete(
    breaks = data$Group,
    labels = paste(data$Group," (", round(data$Value / sum(data$Value) * 100, 2),
                   "%)", sep = "")) +
  scale_fill_manual(values = c("#00468BFF","#ED0000FF","#42B540FF","#0099B4FF")) +
  ggtitle("Pie Plot") + 
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.ticks = element_blank(),
    plot.title = element_text(size = 14, face = "bold",
                              hjust = 0.5, vjust = -1),
    legend.position = "none"
  )
    
  
p
FigureΒ 1: Pie

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%.