我做了这些饼状图:
df <- expand.grid(log.var = c(TRUE, FALSE), zone = 1:4)
df$proportion <- c(0.3, 0.7, 0.4, 0.6, 0.2, 0.8, 0.5, 0.5)
df$size = sample(1:20, 8)
library(ggplot2)
ggplot(df, aes(factor(1), proportion, fill = log.var)) +
geom_bar(stat = "identity") + coord_polar(theta = "y") + facet_grid(.~zone)
是否有调整根据总和的每个饼图的大小的任何size
在每个zone
?
@ lukeA的建议是合理的,但完全不是那么回事:
library("ggplot2"); theme_set(theme_bw())
library("dplyr") ## for mutate()
set.seed(101)
df <- expand.grid(log.var = c(TRUE, FALSE), zone = 1:4)
df <- mutate(df,
proportion=c(0.3, 0.7, 0.4, 0.6, 0.2, 0.8, 0.5, 0.5),
size = sample(1:20, 8),
totsize=ave(size, zone,FUN=sum))
g0 <- ggplot(df, aes(x=factor(1), y=proportion, fill = log.var))
g0 + geom_bar(stat="identity",aes(width=totsize))+facet_grid(.~zone)+
coord_polar(theta = "y")
这里的问题是,该杆在X轴的中间绘制(在直角坐标); 我们希望他们能够从0到运行的全宽度的x坐标绘制的,但我不知道该怎么做。 另一种方法是做一堆累积比例/堆叠手工计算(或至少外ggplot2
),然后使用geom_rect()
...
这是如何做:
df <- df %>% group_by(zone) %>%
mutate(cp1=c(0,head(cumsum(proportion),-1)),
cp2=cumsum(proportion))
ggplot(df) + geom_rect(aes(xmin=0,xmax=totsize,ymin=cp1,ymax=cp2,
fill=log.var)) + facet_grid(.~zone)+
coord_polar(theta = "y")