我想使用兩個並排的餅圖使用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
編輯:Changing 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'如[這裏]顯示(http://stackoverflow.com/questions/18537378/faceted-piechart-with-ggplot2)? – aosmith
'position_stack'將標籤放在正確的位置,不起作用 –
您試過了嗎?因爲我發現'position_fill'能夠很好地將文本放在正確的位置。 – aosmith