2016-09-23 3 views
1

stripchartを使用して散布図を重ねたバープロットがあります。バープロット上にストリップチャートをオーバーレイする - 列をどのように整列するのですか?

here:ここに示されているように

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

しかしながら、散布上の点は、barplot上のバーがずれています。

散布図の間隔を変更して一致させるにはどうすればよいですか?私が理解しているように、はbarplotのようにspaceまたはwidthという変数を持っていません。

答えて

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を使用して、同じ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

+0

おかげでそんなに、最初のものは完全に働きました! – 8eastFromThe3ast

関連する問題