使用 ggplot2 的并排饼图

Side-By-Side Pie Charts With ggplot2

我的数据在不同地理区域具有 5 年的值,我正在尝试生成一个并排饼图来显示每年的区域份额。

我试过下面的代码,看起来效果不错,但饼图不完整。请查看图片:Link to the chart

我不确定我错过了什么,非常感谢任何帮助!

这是我使用的代码:

library(reshape)
library(ggplot2)

test1<-melt(comp)

ggplot(test1, aes(x = factor(1), y = value, fill = factor(variable))) + 
geom_bar(stat = "identity", width = 1) + 
theme(legend.position = "none") +
scale_x_discrete(NULL, expand = c(0,0)) +
scale_y_continuous(NULL, expand = c(0,0)) + 
coord_polar(theta = "y") +
facet_wrap(~Year)

这是我使用的数据:

comp<-structure(list(World = c(169, 187, 210, 226, 232, 261, 245, 223, 
195, 174, 154, 163, 195, 221, 240, 264, 283, 296, 269, 256, 231, 
214, 201, 171, 200, 216, 234, 253, 282, 305, 296, 279, 266, 243, 
232, 203, 216, 239, 266, 284, 315, 340, 319, 304, 277, 250, 228, 
213, 240, 251, 281, 298, 322, 350, 330, 311, 289, 265, 239, 219
), `NA` = c(102, 115, 128, 136, 137, 151, 140, 128, 103, 96, 
84, 99, 123, 141, 152, 163, 178, 170, 153, 146, 131, 125, 118, 
96, 112, 117, 126, 138, 152, 163, 156, 148, 143, 131, 128, 107, 
110, 123, 138, 150, 169, 181, 169, 160, 141, 123, 112, 105, 121, 
126, 148, 155, 168, 183, 170, 158, 149, 136, 121, 108), SA = c(12, 
13, 15, 16, 17, 19, 18, 16, 15, 14, 11, 9, 10, 13, 16, 20, 22, 
28, 25, 23, 20, 16, 13, 11, 15, 18, 20, 23, 26, 30, 28, 26, 24, 
21, 18, 15, 19, 23, 26, 30, 33, 37, 34, 32, 29, 26, 24, 23, 26, 
28, 31, 35, 39, 43, 41, 38, 33, 30, 26, 23), Eur = c(52, 55, 
61, 67, 73, 82, 80, 76, 73, 62, 59, 54, 59, 62, 66, 70, 75, 86, 
81, 79, 73, 68, 66, 61, 66, 71, 76, 79, 85, 91, 89, 86, 82, 76, 
73, 70, 74, 79, 83, 88, 91, 95, 92, 90, 87, 83, 77, 74, 80, 82, 
85, 89, 95, 98, 95, 93, 89, 85, 80, 76), Pac = c(3, 4, 6, 7, 
5, 9, 7, 3, 4, 2, 0, 1, 3, 5, 6, 11, 8, 12, 10, 8, 7, 5, 4, 3, 
4, 6, 9, 11, 14, 15, 18, 15, 13, 12, 10, 7, 8, 10, 13, 11, 15, 
19, 17, 15, 14, 12, 10, 7, 8, 10, 12, 13, 12, 15, 14, 13, 11, 
8, 7, 7), China = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 2, 5, 6, 5, 4, 4, 3, 3, 
4, 5, 4, 6, 5, 7, 8, 7, 7, 6, 6, 5, 4, 5, 5, 5, 6, 8, 11, 10, 
9, 7, 6, 5, 5), Year = c("2010", "2010", "2010", "2010", "2010", 
"2010", "2010", "2010", "2010", "2010", "2010", "2010", "2011", 
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2012", "2012", "2012", "2012", "2012", 
"2012", "2012", "2012", "2012", "2012", "2012", "2012", "2013", 
"2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013", 
"2013", "2013", "2013", "2014", "2014", "2014", "2014", "2014", 
"2014", "2014", "2014", "2014", "2014", "2014", "2014")), class =     
"data.frame", row.names = 3:62)

有几个问题。

首先,您在比较 World 时就好像它是一个地区,就像中国或欧洲或其他地区一样。我几乎可以肯定您只想添加区域,因此当我将其重塑为长数据时,我将取出 World 列。如果您以后需要引用它,您可以选择保留它并从 gather 中省略它。

其次,您有一个名为 NA 的列。这是个坏主意,因为 NA 在 R 中有非常具体的含义。现在我正在重命名它 no_name,但也许是北美?

第三,这里没有将条形相加成一个完整圆圈的机制,所以它们不会。制作条形图时使用 position = "fill"

此外 geom_col 等同于 geom_bar(stat = "identity"),特别是因为我需要确保柱状图正确相加,所以我认为 geom_col 会更安全。

在第一个版本中,条形只是堆叠在一起,您可能会看到楔形之间的边界。在我看来,这有点丑陋,所以对于第二个,我首先将每个年份和地区的值相加。

library(tidyverse)

df_long <- df %>%
  rename(no_name = `NA`) %>%
  select(-World) %>%
  gather(key = region, value = value, -Year)

ggplot(df_long, aes(x = factor(1), y = value, fill = region)) +
  geom_col(width = 1, position = "fill") +
  coord_polar(theta = "y") +
  facet_wrap(~ Year) +
  scale_fill_discrete(guide = F)

df_sums <- df_long %>%
  group_by(Year, region) %>%
  summarise(value = sum(value))

ggplot(df_sums, aes(x = factor(1), y = value, fill = region)) +
  geom_col(width = 1, position = "fill") +
  coord_polar(theta = "y") +
  facet_wrap(~ Year) +
  scale_fill_discrete(guide = F)

reprex package (v0.2.0) 创建于 2018-05-23。