使用基础图形,可以使用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])
下面是一个使用GGPLOT2相同的两个地块的版本。
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()
感谢这么多,第一个非常完美! – 8eastFromThe3ast