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)
产生
我想将平均值和误差条略微偏向右侧,这样误差条就不会遮挡箱线图的IQR线。指定position=position_dodge(.5)
不会似乎无效,因为geom_errorbar
不了解geom_boxplot
。
答案 0 :(得分:1)
您可以引入一个新变量,用作错误栏的x偏移量:
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))
as.numeric(as.factor(.))
确保新的错误条与箱图的位置相同但移动了0.5个单位。