jok*_*kel 1 r bar-chart ggplot2
我想绘制一个带有积分的"组合"条形图.考虑遵循虚拟数据:
library(ggplot2)
library(gridExtra)
library(dplyr)
se <- function(x){sd(x)/sqrt(length(x))}
p1 <- ggplot(mtcars, aes(y=disp, x=cyl, fill=cyl))
p1 <- p1 + geom_point() + theme_classic() + ylim(c(0,500))
my_dat <- summarise(group_by(mtcars, cyl), my_mean=mean(disp),my_se=se(disp))
p2 <- ggplot(my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se))
p2 <- p2 + geom_bar(stat="identity",width=0.75) + geom_errorbar(stat="identity",width=0.75) + theme_classic() + ylim(c(0,500))
Run Code Online (Sandbox Code Playgroud)
最终的情节看起来应该是这样的:

您可以将图层一起添加,但如果它们具有不同的数据和/或美学,则您需要在每个图形图层中包含data和aes参数.
p3 <- ggplot() +
geom_bar(data=my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se), stat="identity", width = 0.75) +
geom_errorbar(data=my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se), width = 0.75) +
geom_point(data=mtcars, aes(y=disp, x=cyl, fill=cyl)) +
ylim(c(0,500)) +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
如果你想使点数偏离柱的一侧,你可以从圆柱值中减去一个偏移量来移动点.像@LukeA提到的那样,通过将geom_point更改为geom_point(data=mtcars, aes(y=disp, x=cyl-.5, fill=cyl)).