ggplot:带填充和标准错误的箱线图
ggplot: Boxplot with fill and standard errors
这是一个箱线图
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + geom_boxplot()
我想重现显示均值、一个标准误差和两个标准误差而不是中位数和分位数的同一图。
我做到了
boxes <- function(x) {
#r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
meanx = mean(x)
n = length(x)
se = sqrt(var(x)/n)
r <- c( meanx-1.96*se, meanx-se, meanx, meanx+se, meanx+1.96*se )
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + stat_summary(fun.data=boxes, geom="boxplot")
问题是不同颜色的方框重叠而不是并排。
position = 'dodge' 应该可以解决你的问题。
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + stat_summary(fun.data=boxes, geom="boxplot", position = 'dodge')
这是一个箱线图
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + geom_boxplot()
我想重现显示均值、一个标准误差和两个标准误差而不是中位数和分位数的同一图。
我做到了
boxes <- function(x) {
#r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
meanx = mean(x)
n = length(x)
se = sqrt(var(x)/n)
r <- c( meanx-1.96*se, meanx-se, meanx, meanx+se, meanx+1.96*se )
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + stat_summary(fun.data=boxes, geom="boxplot")
问题是不同颜色的方框重叠而不是并排。
position = 'dodge' 应该可以解决你的问题。
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + stat_summary(fun.data=boxes, geom="boxplot", position = 'dodge')