连接误差棒的平均点 [英] Connect mean points of error bars

查看:218
本文介绍了连接误差棒的平均点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot2中,我尝试了一个简单的事情,我因为某种原因无法获得。我调整了数据框中的平均值和SE,并想绘制平均值,误差线,然后用平均值连接平均值。这是代码和错误(除了连接 geom_line (与 RCookbook

  #data set 
data1 < - structure(list(group = structure(1:3,.Label = c(1,2,3 (
),class =factor),估计值= c(55.7466654122763,65.0480954172939,
61.9552391704298),SE = c(2.33944612149257,2.3243565412438,
2.33754952927041),t.ratio = c ),.Names = c(group,estimate,SE,t.ratio
),row.names = c(NA,3L) ,class =data.frame)

#尝试图
pd < - position_dodge(.1)
ggplot(data1,aes(x = group,y =估计,group = group))+
geom_errorbar(aes(ymin =估计-SE,ymax =估计+ SE),
color =black,width = .1,position = pd)+
geom_line( data = data1,aes(x = group,y = estimate))+
geom_point(position = pd,size = 4)

错误:

  ymax未定义:使用y调整位置
geom_path :每个组只包含一个观察值。你需要调整团体审美吗?


解决方案

如果您通过 ggplot调用中的group ,并在调用 geom_line x = as.numeric(group) c $ c>,它工作。

此外,您不需要重新引用 data1 geom_line

  ggplot(data1,aes(x = group,y =估计))+ 
geom_errorbar(aes(ymin =估计-SE,ymax =估计+ SE),
color =black,width = .1,position = pd)+
geom_line (aes(x = as.numeric(group),y = estimate))+
geom_point(position = pd,size = 4)



如果 group 由 group ,那么您只有一个值 geom_line 来创建一行,从而产生错误信息。如果 ggplot 正在考虑 x y 映射变量作为一个因素 - 这是因为如果你编写一个变量作为因子R(和ggplot)将它们视为独立组,并且不连接点 - 这是合理的默认行为。



编辑 - 带有字母因子标签



由于内部编码因子的方式,这将与字母 R (即as.numeric(因子)返回数字而不是因子标签)

ie.e



将组更改为 a b c

  levels(data1 [['group']])<  -  letters [1 :
ggplot(data1,aes(x = group,y = estimate))+
geom_errorbar(aes(ymin =估计SE,ymax =估计+ SE),
color = black,width = .1,position = pd)+
geom_line(aes(x = as.numeric(group),y = estimate))+
geom_point(position = pd,size = 4 )


In ggplot2 I'm attempting a simple thing that I just can't get for some reason. I have adjusted means and SE in a data frame and want to plot the means, error bars and then connect the means with points. Here's the code and the error (it does everything except connect the means with geom_line (working with RCookbook:

library(ggplot2)
#data set
data1 <- structure(list(group = structure(1:3, .Label = c("1", "2", "3"
), class = "factor"), estimate = c(55.7466654122763, 65.0480954172939, 
61.9552391704298), SE = c(2.33944612149257, 2.33243565412438, 
2.33754952927041), t.ratio = c(23.8290016171476, 27.8884844271143, 
26.5043535525714)), .Names = c("group", "estimate", "SE", "t.ratio"
), row.names = c(NA, 3L), class = "data.frame")

#the attempted plot
pd <- position_dodge(.1)
ggplot(data1, aes(x=group, y=estimate, group=group)) + 
    geom_errorbar(aes(ymin=estimate-SE, ymax=estimate+SE), 
        colour="black", width=.1, position=pd) +
    geom_line(data=data1, aes(x=group, y=estimate)) + 
    geom_point(position=pd, size=4)

the error:

ymax not defined: adjusting position using y instead
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

解决方案

If you remove the grouping by group within the ggplot call and set x = as.numeric(group ) within the call to geom_line, the it works.

Also, you don't need to re-reference data1 within geom_line

ggplot(data1, aes(x=group, y=estimate)) + 
  geom_errorbar(aes(ymin=estimate-SE, ymax=estimate+SE), 
  colour="black", width=.1, position=pd) +
  geom_line( aes(x=as.numeric(group), y=estimate)) + 
  geom_point(position=pd, size=4)

If you group by group, then you only have one value for geom_line to create a line from, hence the error message. The same error occurs if ggplot is considering the x or y mapping variables as a factor - this is because if you code a variable as a factor R (and ggplot) will consider them independent groups, and not connect the points - this is sensible default behaiviour.

EDIT - with alphabetic factor labels

This will work with alphabetic factor labels due to the way factors are coded internally by R (ie as.numeric(factor) returns numbers not the factor labels)

ie.e

Changing group to a, b, c

levels(data1[['group']]) <- letters[1:3] 
ggplot(data1, aes(x=group, y=estimate)) + 
  geom_errorbar(aes(ymin=estimate-SE, ymax=estimate+SE), 
  colour="black", width=.1, position=pd) +
  geom_line( aes(x=as.numeric(group), y=estimate)) + 
  geom_point(position=pd, size=4)

这篇关于连接误差棒的平均点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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