【发布时间】:2017-02-27 15:16:56
【问题描述】:
我想使用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
编辑: 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,如图所示here? -
position_stack将标签放在正确的位置,这是行不通的 -
你试过了吗?因为我发现
position_fill把文字放在正确的位置相当不错。 -
我已经尝试过了,但如果您认为您有解决方案,请提供可重现的答案。我提供了问题中的所有数据
-
一个有效的方法是
facet_grid(City ~ ., scales = "free")但是这会产生垂直堆叠的饼图,而我需要水平的饼图