多个ggplot饼图与整个派 [英] Multiple ggplot pie charts with whole pies
问题描述
我想使用 ggplot2
制作两张并排饼图,但在制作饼图整个时都有困难
这是我的一个样本数据。
>测试
纽约柏林集团
1 474 755未参观
2 214 123曾参观一次
3 66 122参观> 1
4 142 64已存在
当我尝试时:
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') +指南(fill = guide_legend(nrow = 2,byrow = TRUE))
pie
但是这产生了:
任何关于如何生成两个水平的饼图的建议?
<这里是数据:
> dput(melted2)
structure(list(Type = structure(c(1L,4L,3L,2L,1L,4L,3L,
2L),.Label = c(Never Visited (1L,1L,1L,1L,2L,
2L,2L,1L,1L,1L,2L,1L,1L,1L,2L, ,2L),.Label = c(纽约,柏林),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)
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))
I would like to make two side by side pie charts using ggplot2
but am having difficulty making both pie charts "whole"
Here is a sample of my data.
> test
New York Berlin group
1 474 755 Never Visited
2 214 123 Visited Once
3 66 122 Visited > 1
4 142 64 Resided
When I try:
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
But this produces:
EDIT: Changing facet_grid(facets=. ~ City)
to facet_grid(City ~ ., scales = "free")
works but it produces vertically stacked charts like this:
Any suggestions on how to produce two whole pie charts that are horizontal?
Here is the data:
> 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")
T show relative proportions for each facet, one option is to use position_fill
. It works for both the bars and the text stacking.
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))
这篇关于多个ggplot饼图与整个派的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!