ぼやかないつもりのメモ(ブログ Ver)

つぶやきとメモの記録。更新はぼちぼち。

作図いろいろ

学会発表用にいろいろと作図したので、まとめておく。結局「どれが好きか」という問題に近い感じ。最終的に発表には、points()関数とjitter()関数の合わせ技で作った図が使われました。個人的には、箱ヒゲ図+(points()関数とjitter()関数の合わせ技)が好きです。ヴァイオリンプロットとdotプロットを合わせるのは悪くなかったけど、データ数が多すぎて、うまく制御できませんでした(irisデータだったらできた)。そして、いざエントリを投稿しようと思ったら、stripchart()関数を思い出した。結局、stripchart()関数を思い出していれば、今回の目的には一番早かったので、ちょっと残念。一応、コードをまとめて晒してみる(データはirisを使用)。

data("iris")
colors <- c("black", "red", "springgreen3", "royalblue2", "deepskyblue2", "hotpink2", "darkorange2", 
            "darkcyan", "gray30", "slateblue4")

# ヴァイオリンプロット(ggplot2)
library(ggplot2)
pl <- ggplot(iris, aes(y = Sepal.Length, x = Species))
pl + geom_violin(aes(color = Species))

# ヴァイオリンプロットと箱ヒゲ図
pl + geom_violin(aes(color = Species)) +
  geom_boxplot(aes(color = Species), width = 0.1)

# ヴァイオリンプロットとdotプロット
pl + geom_violin(aes(color = Species)) + 
  geom_dotplot(aes(fill = Species, color = Species), binaxis = "y",
               stackdir = "center", stackratio = 1.5,
               binwidth = 0.2, dotsize = 0.2)

# points()関数とjitter()関数の合わせ技
plot(iris$Sepal.Length, type = "n",
     xlim = c(0.5, 3.5), axes = FALSE,
     xlab = "", ylab = "", main = "")
axis(1, at = 1:3, labels = levels(iris$Species))
axis(2)
points(jitter(as.numeric(iris$Species)), iris$Sepal.Length,
       pch = 16, col = colors[iris$Species])
box()

# 箱ヒゲ図+(points()関数とjitter()関数の合わせ技)
boxplot(iris$Sepal.Length ~ iris$Species,
        border = colors[seq(along.with = iris$Species)])
points(jitter(as.numeric(iris$Species)), iris$Sepal.Length,
       pch = 16, col = colors[iris$Species])

# ストリップチャート
stripchart(iris$Sepal.Length ~ iris$Species, 
           method = "jitter", vertical = TRUE,
           pch = 16, col = colors[seq(along.with = iris$Species)])