以前の日記で少し前フリしていた色の話。
色は視認性が高いグラフを書くときに大切な要素です。
ですが、色を多用すると色彩に目が行き、グラフが持つ情報の本質がなくなってしまう。
僕もちゃんと色を使いこなせてないのですが、人が理解できる限界は3~4色じゃないかと思ってます。
- Rのデフォルト
多くの関数で色は引数colで指定できるはず。
Rはデフォルトで8つの色が数値1~8にわり当てられていて、pallete関数で確認できます。
> dat <- matrix(rep(1,8)) > palette() # デフォルトのカラーコード [1] "gray" "yellow" "magenta" "cyan" "blue" "green3" "red" [8] "black" > > jpeg("defaulfcol.jpg") > layout(1:2) > par(mai=c(.8,.1,.4,.1)) > barplot(dat,horiz=TRUE,col=1:8,xaxt="n",main="col=1~8") # > axis(side=1,at=cumsum(dat)-.5,labels=cumsum(dat),font=2) > barplot(dat,horiz=TRUE,col=palette(),xaxt="n",main="palette()") > axis(side=1,at=cumsum(dat)-.5,labels=cumsum(dat),font=2) > dev.off()
デフォルトのカラーコードを買えたいときはpalette関数を書き換えます。
> palette(rev(palette())) # 引数にカラーコードを入れる > palette() # デフォルトからpallete()の逆順に変わっている [1] "gray" "yellow" "magenta" "cyan" "blue" "green3" "red" [8] "black" > > jpeg("defaulfcol2.jpg",height=240) > par(mai=c(.8,.1,.4,.1)) > barplot(dat,horiz=TRUE,col=1:8,xaxt="n",main="col=1~8") # col=1~8が変わってい る > axis(side=1,at=cumsum(dat)-.5,labels=cumsum(dat),font=2) > dev.off() > > palette(rep("red",9)) # これはエラー、引数valueの長さは8まで > > palette("default") # 元に戻す > palette() # 戻っている [1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" [8] "gray"
- colors関数
R起動時に読み込まれるgrDevicesパッケージにはカラーセットがいくつかあります。
colors関数はRが明示的に持っている色のセットです。全部で657色あります。
> str(colors()) chr [1:657] "white" "aliceblue" "antiquewhite" "antiquewhite1" ... > head(colors(),10) [1] "white" "aliceblue" "antiquewhite" "antiquewhite1" [5] "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine" [9] "aquamarine1" "aquamarine2" > > jpeg("colors.jpg") > par(mai=c(0,0,0,0)) > image(matrix(1:657,ncol=9),col=colors(),axes=FALSE) > dev.off()
grDeviceパッケージの他の色関数は次回で。