多个完整的饼图的ggplot饼图

6
我想使用ggplot2制作两个并排的饼图,但是我难以使两个饼图都成为“完整”的。以下是我的数据示例。
> test
  New York Berlin         group
1      474    755 Never Visited
2      214    123  Visited Once
3       66    122   Visited > 1
4      142     64       Resided

当我尝试使用以下代码:

  pie <- ggplot(data = melted2, aes(x = "", y = Cnt, fill = Type )) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") +
  facet_grid(facets=. ~ City)  +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank()) + theme(legend.position='bottom') + guides(fill=guide_legend(nrow=2,byrow=TRUE))

pie

但这将产生:此处输入图片描述 编辑:将facet_grid(facets=. ~ City)更改为facet_grid(City ~ ., scales = "free")可以工作,但它会产生像这样的垂直堆叠图表:此处输入图片描述 有什么建议可以产生两个完整的水平饼图吗?
以下是数据:
> dput(melted2)
structure(list(Type = structure(c(1L, 4L, 3L, 2L, 1L, 4L, 3L, 
2L), .Label = c("Never Visited", "Resided", "Visited > 1", "Visited Once"
), class = "factor"), City = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L), .Label = c("New York", "Berlin"), class = "factor"), 
    Cnt = c(474L, 214L, 66L, 142L, 755L, 123L, 122L, 64L)), row.names = c(NA, 
-8L), .Names = c("Type", "City", "Cnt"), class = "data.frame")

也许你想使用 position_fill 而不是 position_stack,如此所示 here - aosmith
position_stack 将标签放置在正确的位置,但这样做不起作用。 - iskandarblue
1
你试过了吗?因为我发现position_fill可以将文本放在恰当的位置。 - aosmith
我已经尝试过了,但如果您认为有解决方案,请提供可重现的答案。我在问题中提供了所有数据。 - iskandarblue
谢谢,请指出在我提供的代码中插入的位置。 - iskandarblue
显示剩余2条评论
3个回答

10

为了显示每个分面的相对比例,一种选项是使用position_fill。它适用于条形图和文本堆叠。

ggplot(data = melted2, aes(x = "", y = Cnt, fill = Type )) + 
    geom_bar(stat = "identity", position = position_fill()) +
    geom_text(aes(label = Cnt), position = position_fill(vjust = 0.5)) +
    coord_polar(theta = "y") +
    facet_wrap(~ City)  +
    theme(axis.title.x = element_blank(),
          axis.title.y = element_blank()) + 
    theme(legend.position='bottom') + 
    guides(fill=guide_legend(nrow=2, byrow=TRUE))

在此输入图片描述


这可能是最好的选择。也许可以添加 scale_y_continuous(labels = scales::percent)。我看到的另一个选择是放弃分面并组合单独的图表。 - Axeman

2
如果你将比例传递给ggplot2,它会起作用:
library(dplyr); library(ggplot2)
melted2 <- melted2 %>% group_by(City) %>% mutate(per = Cnt/sum(Cnt))
pie <- ggplot(data = melted2, aes(x = "", y = per, fill = Type)) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") +
  facet_grid(facets=. ~ City)  +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank()) + theme(legend.position='bottom') + guides(fill=guide_legend(nrow=2,byrow=TRUE))

pie

enter image description here


0

也许这就是你所寻找的(显示百分比而不是计数):

library(tidyverse)
melted3 <- melted2 %>% group_by(City) %>% mutate(Percent = Cnt / sum(Cnt))

pie <- ggplot(data = melted3, aes(x = "", y = Percent, fill = Type)) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = round(Percent, digits = 2)), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") +
  facet_grid(facets = . ~ City)  +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank()) + theme(legend.position = 'bottom') +     guides(fill = guide_legend(nrow = 2, byrow = TRUE))

不要四舍五入百分比本身,而是四舍五入标签(或者让ggplot使用position_fill来处理)。 - Axeman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接