ggplot和dplyr显示标准错误

时间:2017-03-06 21:03:48

标签: r ggplot2 dplyr

我有一个情节,我绘制多条线,每个点我想显示误差条。我需要使用

geom_errorbar(aes(ymax=ymax, ymin=ymin), width=0.25) + xlab('points') 

我的问题是如何最好地将ymax设为ymin列

目前数据框看起来像这样

data1 <- data.frame(
  group=c("A","A","A","A","B","B","B","B"), 
  x= c(1,2,3,4,5,6,7,8),
  y = c(1,2,3,4,5,6,7,8), 
  z= c(10,20,30,40,50,60,70,80))     # sample data matrix

data2 = as.data.frame (data1 %>% group_by( group  ) %>% 
  summarise(
    MU_Y= mean(y),
    upper_limit_Y =MU_Y+(1.96*sd(y, na.rm = TRUE)/sqrt(sum(!is.na(y)))),
    lower_limit_Y = MU_Y-(1.96*sd(y, na.rm = TRUE)/sqrt(sum(!is.na(y)))),
    MU_Z= mean(z),
    upper_limit_Z =MU_Z+(1.96*sd(z, na.rm = TRUE)/sqrt(sum(!is.na(z)))),
    lower_limit_Z = MU_Z-(1.96*sd(z, na.rm = TRUE)/sqrt(sum(!is.na(z))))
  )  %>% 
  gather(key =Metric, value = Value  , 
         #c(MU_Y,lower_limit_Y,upper_limit_Y,MU_Z, upper_limit_Z,lower_limit_Z) )
          c(MU_Y,MU_Z) )
)


  group upper_limit_Y lower_limit_Y upper_limit_Z lower_limit_Z Metric Value
1     A      3.765175      1.234825      37.65175      12.34825   MU_Y   2.5
2     B      7.765175      5.234825      77.65175      52.34825   MU_Y   6.5
3     A      3.765175      1.234825      37.65175      12.34825   MU_Z  25.0
4     B      7.765175      5.234825      77.65175      52.34825   MU_Z  65.0

ggplot(data2, aes(x = group, y= Value, group = Metric ))+
  geom_line()+
  geom_point() 

我需要为数据框ymin和ymax创建2个新列,这些列是适当的上限/下限。添加的新列应如下所示(我没有复制所有小数位):

 ymin    ymax
    1.23..   3.76..
    5.23..   7.76..
    12.34..  37.65..
     52...    77.65...

然后我就可以为每个点绘制线条和误差线。

ggplot(data2, aes(x = group, y= Value, group = Metric ))+
  geom_line()+
  geom_point()  +
  geom_errorbar(aes(ymax=ymax, ymin=ymin), width=0.25) + 
  xlab('points') 

1 个答案:

答案 0 :(得分:1)

我认为你的gather在错误的地方。

data2 <- data1 %>% 
  gather(key = Metric, value = Value, -group, -x) %>%
  group_by(group, Metric) %>%
  summarise(
    MU = mean(Value),
    SD = sd(Value, na.rm = TRUE),
    N = sum(!is.na(Value)),
    upper_limit = MU + SD/sqrt(N),
    lower_limit = MU - SD/sqrt(N)
  )


ggplot(data2, aes(x = group, y= MU, group = Metric ))+
  geom_line()+
  geom_point()  +
  geom_errorbar(aes(ymax=upper_limit, ymin=lower_limit), width=0.25) + 
  xlab('points') 

这样做你想要的吗?