2013-04-22 40 views
3

我已使用stat_summary从我的数据创建了多个条形图。但是,我想手动指定错误栏的限制(而不是使用mean_cl_boot)。如何使用facet_grid对数据进行绘图?在使用facet_grid时指定ggplot2中的自定义错误栏

我用于创建图表中的代码如下:

graph <- ggplot(slantclean, aes(x = View,value, fill = Texture)) 

graph + stat_summary(fun.y = mean, geom = "bar", position = "dodge") + 
    stat_summary(fun.data = mean_cl_boot, geom = "pointrange", 
        position = position_dodge(width = 0.90)) + 
    labs(x = "View", y = "Vertical height of ellipse (cm)", fill = "Texture") + 
    facet_grid(Shape ~ TNOGroup) 

不幸的是,数据的复杂性意味着,一个最小的例子是不可能的。数据帧可以访问here。该图的一个例子是here

+1

你,如果你总结ggplot呼叫(而不是依靠'stat_summary')外的数据可能会更成功。然后,您可以指定当时错误栏的大小,并将该汇总数据用作“geom_pointrange”的数据参数。 – 2013-04-22 18:23:58

+0

我最终做到了这一点,令人惊讶的简单。感谢您的建议。 – luser 2013-04-23 13:35:19

+0

[可以如何编写一个函数来创建用于ggplot2的自定义错误栏?](http://stackoverflow.com/questions/6717675/how-can-one-write-a-function-to-create - 自定义错误酒吧使用与ggplot2) – 2014-01-16 08:01:23

回答

7

您可以在stat_summary()中定义自己的功能。如果在stat_summary()中使用geom="pointrange",则函数应在一个数据帧中给出y,yminymax值。

下面是使功能my.fun计算最小值,最大值和平均值的示例。在stat_summary()中使用时,将针对数据中的每个级别计算此值。

my.fun<-function(x){data.frame(ymin=min(x),ymax=max(x),y=mean(x))} 

graph <- ggplot(slantclean, aes(x = View,value, fill = Texture)) 

graph + stat_summary(fun.y = mean, geom = "bar", position = "dodge") + 
    stat_summary(fun.data = my.fun, geom = "pointrange", position = position_dodge(width = 0.90)) + 
    labs(x = "View", y = "Vertical height of ellipse (cm)", fill = "Texture") + 
    facet_grid(Shape ~ TNOGroup) 

enter image description here

+0

祝贺达到10K @Didzis! – Arun 2013-04-23 10:35:33

+1

@Arun谢谢你所需要的最后一个投票:) – 2013-04-23 10:36:50

+0

我不得不这样做,因为我原来的帖子的评论建议,因为R奇怪地计算了整个团队的意思。但是,这对未来非常有用,thankyoU! – luser 2013-04-23 13:34:43

相关问题