在使用stat_summary和ggplot时更改栏宽 [英] Changing bar width when using stat_summary with ggplot

查看:553
本文介绍了在使用stat_summary和ggplot时更改栏宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot中使用 stat_summary 来绘制条形图。我想改变酒吧的宽度。通常这是通过使用宽度选项完成的。使用预先汇总的数据和 stat =identity 它可按预期工作:

 <$ c $ (x,na,(40,100,50))
se < - 函数(x,na, (数据,组〜。,值=y,c(平均值)=(平均值).rm = T)sd(x,na.rm = na.rm)/ sqrt(长度(x))
data2 < ,se))
ggplot(data2,aes(group,mean,ymin = mean-1.96 * se,ymax = mean + 1.96 * se))+
geom_bar(stat =identity,width = 0.5)+ geom_errorbar(width = 0,size = 2)

然而,数据使用 stat_summary ,条形不会更改宽度,而错误条可以:

  ggplot(data,aes(group,y))+ stat_summary(fun.y =mean,geom =bar,width = 0.5)+ 
stat_summary(fun.data =mean_cl_normal, geom =errorbar,width = 0,size = 2)

即使在使用 stat_summary



时更改横线宽度由于第一个示例有效,因此此questi显然已经有一个解决方法,但是,我真的很想知道是否有任何方法可以用 stat_summary 来完成,因为我经常使用它并且经常更方便。

谢谢!

解决方案

这是主题已知问题 444 235



目前的解决方案是传递 width aes 中c $ c> - 这违背了 ggplot (映射vs设置)的原则,但至少它的工作原理....



请注意,如果您想要没有错误栏的错误栏,您可以使用 linerange

  ggplot(data,aes(group,y))+ 
stat_summary(fun.y =mean, geom =bar,aes(width = 0.5))+
stat_summary(fun.data =mean_cl_normal,geom =linerange,size = 2)


I am using stat_summary in ggplot to plot a bar chart. I would like to change the width of the bars. Usually this is done using the width option. With pre-summarised data and stat="identity it works as expected:

data <- data.frame(group=rep(c("a","b"), 20), y=rnorm(40,100,50))
se <- function(x, na.rm=T) sd(x, na.rm=na.rm)/sqrt(length(x))
data2 <- cast(data, group ~ ., value="y", c(mean, se))
ggplot(data2, aes(group, mean, ymin=mean-1.96*se, ymax=mean+1.96*se)) +
 geom_bar(stat="identity", width=0.5) + geom_errorbar(width=0, size=2)

However, in the same plot on original data using stat_summary, the bars don't change width, while errorbars do:

ggplot(data, aes(group, y)) + stat_summary(fun.y="mean", geom="bar", width=0.5) +
 stat_summary(fun.data="mean_cl_normal", geom="errorbar", width=0, size=2)

Is there a way to change bar width even when using stat_summary?

Since the first example works, this question obviously already has a work-around, however, I would really like to know if there is any way to do it with stat_summary, because I use it a lot and is often more convenient.

Thank you!

解决方案

This is the subject of a known issues 444 and 235

The current solution is to pass width within aes -- this goes against the principles of ggplot (mapping vs setting), but at least it works....

Note that you can use linerange if you want the error bars without erro bars!

ggplot(data, aes(group, y)) + 
 stat_summary(fun.y="mean", geom="bar", aes(width=0.5)) +
  stat_summary(fun.data="mean_cl_normal", geom="linerange",  size=2)

这篇关于在使用stat_summary和ggplot时更改栏宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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