使用facet_grid时,在ggplot2中指定自定义错误栏 [英] Specifying custom error bars in ggplot2 when using facet_grid

查看:139
本文介绍了使用facet_grid时,在ggplot2中指定自定义错误栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 stat_summary 从数据创建了多个条形图。但是,我想手动指定错误栏的限制(而不是使用 mean_cl_boot )。对于使用 facet_grid



进行数据绘制的数据,这怎么做?我用来创建图表的代码如下所示:

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

不幸的是,数据的复杂性意味着最小的例子是不可能的。数据框可以通过此处进行访问。此图的示例是此处

stat_summary()中定义自己的函数。如果你在 stat_summary()中使用 geom =pointrange,那么你的函数应该给 y , ymin ymax 一个数据框中的值。



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

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

图< - 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 =椭圆的垂直高度(cm),fill =Texture)+
facet_grid(Shape〜TNOGroup)


I have created multiple bar charts from my data, using stat_summary. However, I want to manually specify the limits of the error bars (instead of using mean_cl_boot). How can this be done for data graphed using facet_grid?

The code I used to create the charts is below:

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)

Unfortunately, the complex nature of the data means that a minimal example is impossible. The data frame can be accessed here. An example of the plot is here.

解决方案

You can define your own function to use in stat_summary(). If you use geom="pointrange" in stat_summary() then your function should give y, ymin and ymax value in one dataframe.

Here is just example to make function my.fun that calculates minimal, maximal and mean value. When used in stat_summary() this values will be calculated for each level in your data.

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)

这篇关于使用facet_grid时,在ggplot2中指定自定义错误栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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