使用 ggplot2 和分组变量创建圆环图

Creating donut charts with ggplot2 and grouping variables

如果这个问题已经得到解答,我深表歉意,但我尝试了很多帖子中的代码都无济于事。我正在尝试在 ggplot 2 中制作圆环图,这对我来说是新的。它似乎在大多数情况下都有效,但并没有将国家分组在一起,因此每一行在饼图中都有自己的块,而不是将所有英国行放在一起(抱歉,这有点乱)。

这是带有一些示例数据的代码(我实际上有 14 个国家和 1200 行):

  country <- c("Australia", "Australia", "China", "UK", "UK", "UK", "Australia", "New Zealand", "Hong Kong", "India", "India", "Korea", "Malaysia", "UK")
  GAV <- c(32626614, 611751827, 1070038943.77, 1070038990, 611751347, 567751827, 444751827, 611751444, 999751827, 111751827, 222751827, 331751827, 611751844, 611777827)

panel_donut <- data.frame(country, GAV)

删除带有 NA GAV 的行

panel_donut <- panel_donut[!is.na(panel_donut$GAV),]

计算百分比

panel_donut$percentage <-  panel_donut$GAV / sum(panel_donut$GAV)* 100

panel_donut <-  panel_donut[rev(order(panel_donut$percentage)), ]

panel_donut$ymax <-  cumsum(panel_donut$percentage)

panel_donut$ymin <-  c(0, head(panel_donut$ymax, n = -1))

panel_donut

重新排序颜色级别

panel_donut <-  panel_donut[order(panel_donut$country), ]

绘制图表

library(ggplot2)
library(ggrepel)

donut <-  ggplot(panel_donut, aes(fill = country, ymax = ymax, ymin = ymin, xmax = 100, xmin = 80)) +

  geom_rect(colour = "black") +

  coord_polar(theta = "y") + 

  xlim(c(0, 100)) +

  theme(legend.title = element_text(colour = "black", size = 16, face = "bold"), 
    legend.text = element_text(colour = "black", size = 15), 
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank())

donut

目前,我得到了一个甜甜圈图,但所有级别都有单独的块,即英国有 4 个甜甜圈块,而不是将其分组为 1 个。我想知道我的代码哪里出错了导致了这种情况的发生。

在此先感谢您的帮助!

是的,您的主数据框包含您所在国家/地区的多个条目。你需要总结它们。试试这个方法:

library(ggplot2)
library(ggrepel)
library(dplyr)

panel_donut %>% 
  group_by(country) %>% 
  summarise(percentage = sum(percentage)) %>% 
  mutate(ymax = cumsum(percentage), 
         ymin = c(0, head(ymax, n = -1))) %>% 
  ggplot(aes(fill = country, ymax = ymax, ymin = ymin, xmax = 100, xmin = 80)) +
  geom_rect(colour = "black") +
  coord_polar(theta = "y") +
  xlim(c(0, 100)) +
  scale_fill_brewer(palette = "Set1") +
  theme(legend.title = element_text(colour = "black", size = 16, face = "bold"), 
        legend.text = element_text(colour = "black", size = 15), 
        panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank())

输出为:

尽管我建议使用相同的库使用更简单的方法:

data.frame(country, GAV) %>% 
  filter(!is.na(GAV)) %>% 
  mutate(percentage = GAV / sum(GAV) * 100) %>% 
  group_by(country) %>% 
  summarise(percentage = sum(percentage)) %>% 
  mutate(ymax = cumsum(percentage), 
         ymin = c(0, head(ymax, n = -1))) %>% 
  ggplot(aes(fill = country, ymax = ymax, ymin = ymin, xmax = 100, xmin = 80)) +
  geom_rect(colour = "black") +
  coord_polar(theta = "y") +
  xlim(c(0, 100)) +
  scale_fill_brewer(palette = "Set1", guide = "none") +
  theme(legend.title = element_text(colour = "black", size = 16, face = "bold"), 
        legend.text = element_text(colour = "black", size = 15), 
        panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank()) +
  geom_label_repel(aes(label = paste(country, "\n", round(percentage, 1),"%"), 
                       x = 100, 
                       y = (ymin + ymax)/2),
                   inherit.aes = F, 
                   show.legend = F, size = 3) +
  annotate("text", x = 0, y = 0, size = 15, label = "Donut Chart")

输出: