ベースグラフィックでは、points
機能を使用して棒グラフの上にポイントをプロットできます。棒グラフ自体から棒のx位置を取得します。
# Fake data
set.seed(1)
dat = data.frame(group=LETTERS[1:5], y=rnorm(25,20,2))
# Assign the barplot to x so that x will contain the bar positions.
x = barplot(tapply(dat$y, dat$group, FUN=mean), ylim=c(0,1.05*max(dat$y)), col=hcl(240,100, 70))
points(rep(x, table(dat$group)), dat$y[order(dat$group)], pch=21, bg="red")
plot(rep(1:length(unique(dat$group)), table(dat$group)),
dat$y[order(dat$group)], pch=21, bg="blue",
ylim=c(0,1.05*max(dat$y)), xlim=c(0.5,5.5), xaxt="n")
points(1:length(unique(dat$group)),
tapply(dat$y, dat$group, FUN=mean),
pch="\U2013", cex=3, col="red")
axis(side=1, at=1:5, labels=LETTERS[1:5])
![enter image description here](https://i.stack.imgur.com/kJq3J.png)
はここggplot2を使用して、同じ2つのプロットのバージョンだ:私はまた、手段がポイントマーカーではなく、棒でプロットされている別のアプローチを含めました。
library(ggplot2)
ggplot(dat, aes(group, y)) +
stat_summary(fun.y=mean, geom="bar", fill=hcl(240,100,50)) +
geom_point() +
theme_minimal()
ggplot(dat, aes(group, y)) +
geom_point() +
stat_summary(fun.y=mean, geom="point", pch="\U2013",
size=8, colour="red") +
scale_y_continuous(limits=c(0, max(dat$y))) +
theme_bw()
![enter image description here](https://i.stack.imgur.com/FdttC.png)
おかげでそんなに、最初のものは完全に働きました! – 8eastFromThe3ast