読者です 読者をやめる 読者になる 読者になる

ggplot2 の作成手順(一例)

R が苦手だった理由の一つに,グラフィックが気に食わないということがあったが,それを解消してくれたのが ggplot2 だ.とにかく直感的に利用できるし,統計を扱う者にしっくりくる.

ライブラリの読み込み

library(ggplot2)

利用できるデータ形式は,data.frame().
例えばsample.csvをデータとすると

[sample.csv]
"class","x","y"
"1.0","0.535","20.0"
"1.0","6.11","15.0"
"1.0","430.0","10.0"
"2.0","0.487","20.0"
"2.0","22.38","15.0"
"2.0","698.6","10.0"
"3.0","0.401","20.0"
"3.0","10.24","15.0"
"3.0","446.8","10.0"
"4.0","0.227","20.0"
"4.0","4.42","15.0"
"4.0","438.1","10.0"
"5.0","0.514","20.0"
"5.0","7.51","15.0"
"5.0","423.5","10.0"
"6.0","0.543","20.0"
"6.0","12.61","15.0"
"6.0","749.4","10.0"
"7.0","0.667","20.0"
"7.0","10.45","15.0"
"7.0","364.7","10.0"
"8.0","0.658","20.0"
"8.0","15.15","15.0"
"8.0","403.5","10.0"
"9.0","0.521","20.0"
"9.0","4.21","15.0"
"9.0","387.7","10.0"
"10.0","0.218","20.0"
"10.0","4.31","15.0"
"10.0","335.3","10.0"

これをデータフレームとして読み込む.

data <- read.csv("sample.csv",header=TRUE)

いよいよggplot2を使ってプロット.
まずは散布図.

g <- ggplot(data, aes(x,y))
g <- g + geom_point()
pdf("point.pdf",width=8,height=5,family="Japan1GothicBBB")
print(g)
dev.off()

f:id:joker1110:20110626033458p:image:w360
なおepsやpngで保存することもできる.

#epsで保存
postscript("point.eps",width=8,height=5, horizontal=FALSE,paper="special")
print(g)
dev.off()

#pngで保存
png("point.png",width=800,height=500)
print(g)
dev.off()

0あたりに点が集まっているので,x軸を対数軸に変更.

g <- g + scale_x_log10()

f:id:joker1110:20110629020356p:image:w360

軸の数値が見えないため,フォントを大きくする.

g <- g + theme_grey(24)

f:id:joker1110:20110629020803p:image:w360

ついでにグラフタイトルを入れてみる.

g <- g + opts(title="sample plot")

f:id:joker1110:20110629021317p:image:w360

プロット点に色をつける.3段階にデータを分けて.ここでは,y値で分ける.

g <- g + geom_point(aes(colour=y))
g <- g + scale_colour_gradientn(colour=rainbow(7))

せっかくなので7色カラーに.
f:id:joker1110:20110629022228p:image:w360

さらにラインを引いてみよう.classで分けるとして,

g <- g + geom_line(aes(group=class,linetype=class,colour=class))

f:id:joker1110:20110629024217p:image:w360
こんな感じで,レイヤーを次から次へと重ねていける.