Circle Packing

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-04-20

πŸ€– AI Skill β€” Download the Bizard unified skill for your AI assistant ⬇️ Download Skill ZIP
Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Circle Packing 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/circle-packing?lang=en

Circle packing is a visualization method used to display the differences in quantity among different categories.

Setup

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

  • Programming language: R

  • Dependent packages: data.table; jsonlite; packcircles; ggplot2; viridis

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

# Load packages
library(data.table)
library(jsonlite)
library(packcircles)
library(ggplot2)
library(viridis)
sessioninfo::session_info("attached")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.3 (2026-03-11)
 os       Ubuntu 24.04.4 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       UTC
 date     2026-04-21
 pandoc   3.1.3 @ /usr/bin/ (via rmarkdown)
 quarto   1.9.37 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 data.table  * 1.18.2.1   2026-01-27 [1] RSPM
 ggplot2     * 4.0.2.9000 2026-04-14 [1] Github (tidyverse/ggplot2@7d79c95)
 jsonlite    * 2.0.0      2025-03-27 [1] RSPM
 packcircles * 0.3.7      2024-11-21 [1] RSPM
 viridis     * 0.6.5      2024-01-29 [1] RSPM
 viridisLite * 0.4.3      2026-02-04 [1] RSPM

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

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

Data Preparation

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

# convert data structure
packing <- circleProgressiveLayout(data[["v"]], sizetype = "area")
data <- cbind(data, packing)
dat_gg <- circleLayoutVertices(packing, npoints = 50)
colors <- c("#E57164","#F8ECA7","#9389C1","#3F9C78","#769F8D","#E5F9A9","#7CE9A4",
            "#CE9FCA","#78F197","#8BB085","#D88880","#A6E4C3","#F7F6B1","#C5E69A",
            "#F45FDE","#5CF371","#9259CF","#2B6D9B","#F3C096","#EEADBE")
dat_gg$value <- rep(colors, each = 51)

# View data
head(data)
  g   v           x          y   radius
1 A 100  -5.6418958  0.0000000 5.641896
2 B 120   6.1803872  0.0000000 6.180387
3 C  80  -0.2298516 -9.2166458 5.046265
4 D  10  -6.7961670 -7.3357638 1.784124
5 E  60 -12.9322538 -6.8624061 4.370194
6 F  30 -14.3618753  0.4597221 3.090194

Visualization

# Circle Packing
p <- ggplot() +
  geom_polygon(data = dat_gg, aes(x, y, group = id, fill = value), colour = "black", alpha = 0.4) +
  scale_fill_manual(values = magma(nrow(data))) +
  theme_void() +
  theme(legend.position = "none") +
  coord_equal() +
  scale_size_continuous(range = c(2.3, 4.5)) +
  geom_text(data = data, aes(x, y, size = v, label = g), vjust = 0) +
  geom_text(data = data, aes(x, y, label = v, size = v), vjust = 1.2)

p
FigureΒ 1: Circle Packing