- matplot系関数
Rはやっぱり、matrixやdata.frameを使えるのことが一番便利と思っている。
だから、Rを使って「便利だな〜」と始めて思ったのが、matplot関数だったと思う。
colやpchを駆使すれば、これだけで結構、素敵なグラフになったりする。
> x <- cbind("steps"=rep(rep(-2:2,each=5),2) + , "sin"=sin(seq(-pi,pi,length.out=50)) + ,"cos"=cos(seq(-pi,pi,length.out=50))) > jpeg("matplot.jpg") > layout(matrix(1:4,ncol=2)) > par(mai=c(.5,.5,.2,.1)) > matplot(x) > matplot(x, pch=1:4) # pch で点の形状を指定 > matplot(x, type="l", lty=3:1) # lty で線種を指定 > matplot(x, type="h", col=rainbow(4), lwd=2) # lwd で線の太さを指定 > dev.off()
そして、plot関数と同じく、matpoints関数とmatlines関数がある。
> jpeg("matpoints.lines.jpg", height=960) > par(mfrow=c(3,1),mai=c(1,1,.5,.1)) > matplot(x, type="b", pch=20) > > matplot(x, type="l") > matpoints(x, pch=20) > > matplot(x, type="p", pch=20) > matlines(x) > dev.off()
で、こうなる。
type="b"は点と線がはっきり分かれるけど、下の2つは重なる。
これは好みだと思う。僕は下の方が好き。
Rの関数名は体系立っているから、「こういう関数があったから、こっちもこういう名前の関数があるかも」って思いつけるのが便利だったりする。