ggplot2 でグラフの書き方メモ
R でグラフを綺麗に描くためのパッケージ ggplot2 を試していますが、なかなか使いやすいですね。
サンプルデータ iris を使ってグラフを書いてみます。
library("ggplot2") ir <- ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width)) + geom_point(size=4, aes(colour=factor(Species))) + labs(colour = "Species") + theme_bw(18) print(ir)
theme_bw() を使うと背景が白くてすっきりしたグラフになります。しかし theme_bw() はフォントサイズぐらいしか調整ができません。細かい微調整をしたいと思ったのですが、でも以下のように theme() によって、theme_bw() のパラメーターを上書きできるようですね。
ir <- ggplot(data=iris,aes(x=Sepal.Length,y=Sepal.Width)) + geom_point(size=4, aes(colour=factor(Species))) + labs(colour = "Species") + theme_bw(18) + theme(legend.position = c(0.85, 0.15)) print(ir)
この例では凡例の位置を調整しています。
単純なことですが、ちょっとしたコツですね。自分用にメモ。