使用ggplot2和分组变量创建甜甜圈图

时间:2019-04-13 16:41:45

标签: r ggplot2 donut-chart

很抱歉,如果已经回答了这个问题,但是我尝试过很多帖子中的代码都没有用。我正在尝试在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。我想知道我的代码在哪里出错了,导致了在这种情况下。

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

yes, your main dataframe has several entries of your countries. you need to summarise them. try this approach:

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())

output is:

enter image description here

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

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")

输出:

enter image description here