选择线图中多个误差线的方向

时间:2017-05-26 06:06:03

标签: r plot ggplot2 hmisc

使用nbr,我们如何有选择地选择误差线的方向?我有一个4线图,其误差条重叠,我想手动选择哪些应该只出现正或负SE或SD。

这是我到目前为止所拥有的。我首先绘制它没有错误条:

ggplot2

enter image description here

然后我总结数据以获得SE和SD:

require("ggplot2")
ggplot(data.long, 
       aes(y = values, x = within_2, group = interaction(Between, within_1), linetype = within_1)) + 
  stat_summary(geom="line", fun.y="mean") +
  stat_summary(geom="point", fun.y="mean", aes(shape = Between)) + 
  scale_shape_manual(values = c(0, 17)) + 
  labs(x = "xlab", y = "% change")

使用错误栏进行绘图:

require("Hmisc")
data_summary <- summarySE(data.long, measurevar="values", groupvars=c("within_1","within_2", "Between"))  

enter image description here

如何在不同的线条上仅绘制上下误差条以改善绘图视觉效果? Here there's a solution似乎正在路上,但我无法在我的4行情节中重现它。

以下是数据:

  ggplot(data_summary, 
         aes(y = values, x = within_2, group = interaction(Between, within_1), linetype = within_1)) + 
    stat_summary(geom="line", fun.y="mean") +
    stat_summary(geom="point", fun.y="mean", aes(shape = Between)) + 
    scale_shape_manual(values = c(0, 17)) + 
    labs(x = "xlab", y = "% change") +
    geom_errorbar(aes(ymin = values-se, ymax = values+se),
                  width=.2)

1 个答案:

答案 0 :(得分:2)

也许这可以帮到你:

data_summary <- Rmisc::summarySE(data.long, measurevar="values",
                                 groupvars=c("within_1","within_2", "Between"))


#Based on the solution you found. 
data_summary$min <- data_summary$values - ((data_summary$Between=='CT' & data_summary$within_1=='H')|
                                            (data_summary$Between=='TT'& data_summary$within_1=='H'))*
                                            data_summary$se

data_summary$max <- data_summary$values + ((data_summary$Between=='CT' & data_summary$within_1=='N')|
                                            (data_summary$Between=='TT'& data_summary$within_1=='N'))*
                                            data_summary$se

ggplot(data_summary, 
       aes(y = values, x = within_2, group = interaction(Between, within_1), linetype = within_1)) + 
  stat_summary(geom="line", fun.y="mean") +
  stat_summary(geom="point", fun.y="mean", aes(shape = Between)) + 
  scale_shape_manual(values = c(0, 17)) + 
  labs(x = "xlab", y = "% change",title="New columns:min and max") +
  geom_errorbar(aes(ymin = min, ymax = max),
                width=.2)

enter image description here

您仍然可以使用两个geom_errorbar()子集为每个geom_bar得到相同的结果:

ggplot(data_summary, 
   aes(y = values, x = within_2, group = interaction(Between, within_1), linetype = within_1)) + 
 stat_summary(geom="line", fun.y="mean") +
 stat_summary(geom="point", fun.y="mean", aes(shape = Between)) + 
 scale_shape_manual(values = c(0, 17)) + 
 labs(x = "xlab", y = "% change",title="2 geom_errorbar") +
 geom_errorbar(data=with(data_summary,data_summary[which(Between=='CT'& within_1=='H' |
                                                          Between=='TT'& within_1=='H'),]),
            aes(ymin = values-se, ymax = values),
            width=.2)+
 geom_errorbar(data=with(data_summary,data_summary[which(Between=='CT'& within_1=='N' |
                                                          Between=='TT'& within_1=='N'),])
            ,aes(ymin = values, ymax = values+se),
            width=.2)

enter image description here

单边上限

可能的解决方法是将width = 0放在geom_errorbar选项中,并使用geom_segment生成大写字母。

ggplot(data_summary, 
   aes(y = values, x = within_2, group = interaction(Between, within_1), linetype = within_1)) + 

 stat_summary(geom="line", fun.y="mean") +
 stat_summary(geom="point", fun.y="mean", aes(shape = Between)) + 
 scale_shape_manual(values = c(0, 17)) + 
 labs(x = "xlab", y = "% change",title="2 geom_errorbar - One-side caps") +

 #errorbar without caps 

 geom_errorbar(data=with(data_summary,data_summary[which(Between=='CT'& within_1=='H' |
                                                          Between=='TT'& within_1=='H'),]),
            aes(ymin = values-se, ymax = values),width=0)+
 geom_errorbar(data=with(data_summary,data_summary[which(Between=='CT'& within_1=='N' |
                                                          Between=='TT'& within_1=='N'),]),
            aes(ymin = values, ymax = values+se),width=0)+

 #geom_segment for caps 

 geom_segment(data=with(data_summary,data_summary[which(Between=='CT'& within_1=='H' |
                                                           Between=='TT'& within_1=='H'),]),
               aes(y=values-se,yend=values-se,x= as.numeric(within_2)-0.1,xend= as.numeric(within_2)+0.1))+

  geom_segment(data=with(data_summary,data_summary[which(Between=='CT'& within_1=='N' |
                                                           Between=='TT'& within_1=='N'),]),
               aes(y=values+se,yend=values+se,x= as.numeric(within_2)-0.1,xend= as.numeric(within_2)+0.1))

enter image description here

希望这有帮助!