R 使用镶嵌面网格时在ggplot2中指定自定义错误栏
我使用R 使用镶嵌面网格时在ggplot2中指定自定义错误栏,r,ggplot2,R,Ggplot2,我使用stat\u summary从数据中创建了多个条形图。但是,我想手动指定错误条的限制(而不是使用mean\u cl\u boot)。如何对使用facet\u grid绘制的数据执行此操作 我用来创建图表的代码如下: graph <- ggplot(slantclean, aes(x = View,value, fill = Texture)) graph + stat_summary(fun.y = mean, geom = "bar", position = "dodge") +
stat\u summary
从数据中创建了多个条形图。但是,我想手动指定错误条的限制(而不是使用mean\u cl\u boot
)。如何对使用facet\u grid
绘制的数据执行此操作
我用来创建图表的代码如下:
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)
graph您可以在stat\u summary()
中定义自己的函数。如果在stat\u summary()
中使用geom=“pointrange”
,则函数应在一个数据帧中给出y
、ymin
和ymax
值
下面是一个示例,让函数my.fun
计算最小值、最大值和平均值。在stat\u 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)
my.fun如果您在ggplot调用之外汇总数据(不依赖于stat\u summary
),您可能会更成功。然后您可以指定当时错误条的大小,并将汇总数据用作geom_pointrange
的数据参数。谢谢你的建议。在到达10K@Didzis时,可能会重复祝贺!我不得不按照我原来帖子上的评论去做,因为R奇怪地在计算各组的平均数。不过,这对未来非常有用,谢谢!