2016-09-23 21 views
1

我有一个barplot,我用散点图覆盖使用stripchart在barplot上叠加条形图 - 如何对齐列?

Barplot(data1means) 
stripchart(data1, add=TRUE, vertical = TRUE) 

然而,在散点图的点对准相上barplot酒吧,如下所示:

here

那么,如何改变散点图的间距,使它们相匹配呢?据我了解,stripchart没有spacewidthbarplot这样的变量。

回答

2

使用基础图形,可以使用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

下面是一个使用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() 

enter image description here

+0

感谢这么多,第一个非常完美! – 8eastFromThe3ast

相关问题