R: ggplot2を使ってみる(1) [統計]
KuboLog 2009-09-15やRevolutions: ggplot2 and the grammar of graphicsでggplot2が取り上げられている。なんだかおもしろそうなので試してみた。
まずはggplot2パッケージをインストール。依存パッケージがたくさんある。
手始めに散布図から。data(iris)で。
library(ggplot2) data(iris)
qplot()関数を使うと簡単だが、なぜかlegendが出なかったのでggplot()関数を使う。
p <- ggplot(iris, aes(Sepal.Width, Sepal.Length))
散布図を作成。+演算子を使うのが特徴。
p1 <- p + geom_point()
表示する。
print(p1)
iris$Species別に色を変えてみる。
p2 <- p + geom_point(aes(colour = Species))
軸を調整。
p3 <- p2 + xlab("Sepal width") + ylab("Sepal length") + xlim(2, 4.5) + ylim(4, 8)
表示する。
print(p3)
コメント 0