R 调整饼图的大小

R 调整饼图的大小,r,charts,ggplot2,R,Charts,Ggplot2,我制作了这些饼图: 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

我制作了这些饼图:

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)

df@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")

类似于
ggplot(转换(df,sum=with(df,ave(大小,区域,FUN=sum))),aes(因子(1),比例,填充=log.var,宽度=sum))+…
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")