2017-02-27 58 views
0

我想使用两个并排的饼图使用ggplot2,但我很难制作两个饼图“整体” 下面是我的数据样本。多个ggplot饼图与整个饼

> 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 

但是,这会产生:enter image description here

编辑:Changing facet_grid(facets=. ~ City)facet_grid(City ~ ., scales = "free")的作品,但它产生的垂直堆叠的图表所示:

enter image description here

有关如何生成两个水平的饼图的任何建议?

这里是数据:每个面

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

也许你想'position_fill'而不是'position_stack'如[这里]显示(http://stackoverflow.com/questions/18537378/faceted-piechart-with-ggplot2)? – aosmith

+0

'position_stack'将标签放在正确的位置,不起作用 –

+1

您试过了吗?因为我发现'position_fill'能够很好地将文本放在正确的位置。 – aosmith

回答

4

T显示相对比例,一个选择是使用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)) 

enter image description here

+0

这可能是最好的选择。也许增加'scale_y_continuous(labels = scales :: percent)'。我看到的其他选择是放弃小平面并合并单独的地块。 – Axeman

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)) 
+0

不要将百分比本身四舍五入,而要将标签四舍五入(或让ggplot用'position_fill'处理)。 – Axeman

+0

@Axeman,根据您的反馈进行更新。 – jsb

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