2014-10-09 73 views
0
library(ggplot2) 
library(Hmisc) 
data(mtcars) 
myplot <- ggplot(mtcars, aes(x = as.factor(cyl), y = qsec)) + 
    geom_boxplot() + 
    stat_summary(fun.y = mean, geom = "point", shape = 5, size = 2) + 
    stat_summary(fun.data = mean_cl_normal, geom = "errorbar", 
      width = 0.2) 

产生 plot躲避箱图和误差线与GGPLOT2

我想躲闪的均值和误差条有点权,使得误差线不掩盖IQR线的boxplot。指定position=position_dodge(.5)似乎不起作用,因为geom_errorbar不知道geom_boxplot

回答

1

您可以介绍您作为X你errorbars偏移使用一个新的变量:

library(ggplot2) 
library(Hmisc) 
data(mtcars) 
mtcars$cyl.n <- as.numeric(as.factor(mtcars$cyl)) + .5 

(myplot <- ggplot(mtcars, aes(x = as.factor(cyl), y = qsec)) + 
    geom_boxplot() + 
    stat_summary(aes(x = cyl.n), fun.y = mean, geom = "point", shape = 5, size = 2) + 
    stat_summary(aes(x = cyl.n), fun.data = mean_cl_normal, geom = "errorbar", 
      width = 0.2)) 

enter image description here

as.numeric(as.factor(.))可确保新的错误栏显示在相同的位置的间隔箱形图,但移动了0.5个单位。

+0

这是一个整洁的想法,并解决了我的问题。谢谢! – coanil 2014-10-09 14:54:28