2017-02-27 59 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