Triad sou.

平均値の折れ線グラフにヒゲを付ける

あまり好きなグラフではないのですが、axis 関数を使って各時点のサンプルサイズを付加する事ができるようなので、やってみました。
Rには gplots::plotmeans と Rcmdr::plotMeans という関数がありますが、一部のグラフオプションが使えなかったりするので、plot 関数を使っています。

path <- "C:/"

##############################
# ダミーデータ生成
n    <- c(30, 29, 27, 25, 16)
mean <- c(20, 19, 17, 15, 15)
sd   <- rep(3, 5)
rep  <- 1

x <- do.call("rbind", 
  mapply(function(n, mean, sd, rep) matrix(rnorm(rep * n, mean = mean, sd = sd),
    nrow=n, ncol=rep), n, mean, sd, rep, SIMPLIFY = FALSE)
)
g <- rep(1:length(n), n)


##############################
# グラフオプション
windowsFonts(Arial="Arial")
windowsFonts(Calibri="Calibri")

png(
  file=paste(path, "MeanPlot.png", sep=""),
  width = 950, height = 600, pointsize = 30
)
par(family="Calibri")
par(bty="l", mar=c(3.2, 3.2, 1.5, 0.5), mgp=c(2.0, 0.7, 0), cex.main=1.1)

# +- SD の場合
mv <- tapply(x, g, mean)
sv <- tapply(x, g, sd)
lc <- mv-sv
uc <- mv+sv
nv <- tapply(x, g, length)
plot(
  1:5, mv, type="b", cex=1, pch=16, ylim=c(0, max(uc)),
  main=NA, ylab="Measurement (unit)", xlab="",
  col = "#E64B6B", lwd=4, xaxt="n", xlim = c(0.5, 5.5),
  panel.first = grid(NA, NULL, lty = 2, col = "#E9DECA")
)
arrows(
  1:5, uc, 1:5, lc,
  length=0.35, angle=90, code=3, lwd=4, col = "#E64B6B"
) 
axis(1, 1:5, c("period 1", "period 2", "period 3", "period 4", "period 5"))
axis(1, 1:5, c(
   expression(paste("(", italic(n), " = 30)")),
   expression(paste("(", italic(n), " = 29)")),
   expression(paste("(", italic(n), " = 27)")),
   expression(paste("(", italic(n), " = 25)")),
   expression(paste("(", italic(n), " = 16)"))
  ), pos=-3, lwd=0
)
dev.off()



データが全ての時点で左右対称で、はずれ値が無ければ妥当なグラフだと思います。
よく分からない場合は箱ひげ図を使おう。


良く聞く意見

  1. 箱ひげ図なんて使われているのを見たことがない、伝統的に使われているし、査読コメントでケチを付けられたくないから私は平均値ヒゲグラフを使います。
  2. 各点のデータ数が少ないし、これで何とかなりませんか。
  3. 箱ひげ図を使うとバラツキが大きく見えて印象が悪い、だから平均値ヒゲグラフにするし、ヒゲは2×SEにします。

平均棒ヒゲグラフよりはマシかなと思います。