色の扱い
1.基本
Rの色指定は、#で始まる16進文字列が基本です。rgb(32,64,96,maxColorValue=255)は、"#204060"を返します。(colorspaceパッケージには大文字のRGB()があるので注意。)
この文字列は、plot() の col= などの指定に使えます。
RGBの数値表現は、[0..1] が、標準らしく、maxColorValue=255 の指定が必要です。
ただし、col2rgb("skyblue") の表示は、[0..255] になっています
- > # rgb()は文字列を返します(maxColorValue=255が必要)
- > rgb(135,206,235,maxColorValue=255)
- [1] "#87CEEB"
- > # R の color(16進文字列) から R,G,Bの値を得ます
- > # 結果は、[0..255]になります
- > col2rgb("#87CEEB")
- [,1]
- red 135
- green 206
- blue 235
- > col2rgb(rgb(135,206,235,maxColorValue=255))
- [,1]
- red 135
- green 206
- blue 235
- > # 色名からR,G,Bの値を得ます
- > col2rgb("skyblue")
- [,1]
- red 135
- green 206
- blue 235
2.色の自動選択
作図では、色分けをしますが、自動的に任意の個数の色が作れます。
 |
- > # rainbow()の色見本
- > n <- 8
- > image(0:n,0:0,matrix(1:n,ncol=1),col=rainbow(n),
- + main=paste("rainbow(",as.integer(n),")"),
- + axes=F,xlab="",ylab="")
|
|