调整饼图的大小 [英] Adjust size of pie charts

查看:312
本文介绍了调整饼图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  df<  -  expand.grid(log.var = c(TRUE (0.3,0.7,0.4,0.6,0.2,0.8,0.5,0.5)
df $ size = sample(1:4),zone = 1:4)
df $比例< 20,8)

library(ggplot2)
ggplot(df,aes(factor(1),proportion,fill = log.var))+
geom_bar(stat =身份)+ coord_polar(theta =y)+ facet_grid(。〜zone)



有什么方法可以调整每个饼图的大小根据区域大小的总和



 <$ c $解析方案

C>库( GGPLOT2); theme_set(theme_bw())
library(dplyr)## mutate()
set.seed(101)
df< - expand.grid(log.var = c TRUE,FALSE),zone = 1:4)
df < - 变异(df,
比例= 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 =因子(1),y =比例,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比例))

ggplot(df)+ geom_rect(aes(xmin = 0,xmax = totsize,ymin = cp1,ymax = cp2,
fill = log.var))+ facet_grid( 。〜zone)+
coord_polar(theta =y)


I've made these pie charts:

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)

Is there any way of adjusting the size of each pie chart according to the sum of size in each zone?

解决方案

@lukeA's suggestion is sensible but doesn't quite work:

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

The problem here is that the bars are drawn (in rectangular coordinates) in the middle of the x-axis; we'd like them to be drawn with their x-coordinates running from 0 to the full width, but I'm not sure how to do that. The alternative would be to do a bunch of the cumulative proportion/stacking computations by hand (or at least outside ggplot2), then use geom_rect() ...

Here's how:

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

这篇关于调整饼图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆