written by S. AOKI, Jul 04, 2003

color.chart(5,5)

等とすることにより,以下のようなカラーチャートを作成する関数です 第一引数は列数,第二引数は行数。デフォールトはそれぞれ8と10。 半分決めうちしてますので,プロットウインドウの大きさも一緒に調整してください。 一度全部描画して,スクリーンショットを取っておくといいかもしれません。

colors.png
polygon.plot <- function(row, col, name, name.of.color, height, cell.width, cell.height)
{
	row <- row*cell.width
	col <- col*cell.height
	ordinate.x <- c(row, row, row+cell.width, row+cell.width, row)
	ordinate.y <- height-c(col, col+cell.height, col+cell.height, col, col)
	polygon(ordinate.x, ordinate.y, col=name.of.color)
	text(row+cell.width*0.1, height-(col+cell.height*0.6), name, pos=4)
	text(row+cell.width*0.1, height-(col+cell.height*0.4), name.of.color, pos=4)
	text(row+cell.width*0.1, height-(col+cell.height*0.2), 
            name.of.color,pos=4,col="white")
}
color.chart <- function(n.width=8, n.height=10)
{
	width <- 640
	height <- 480
	name.of.color <- colors()
	n <- length(name.of.color)
	max <- floor(n/(n.width*n.height))
	old <- par(ask=TRUE)
	for (i in 0:max) {
		plot(c(0, width, width, 0, 0), c(0, 0, height, height, 0),type="n", xaxt="n",      
                      yaxt="n", xlab="",ylab="")
		for (col in 0:(n.height-1)) {
			for (row in 0:(n.width-1)) {
				name <- (n.width*n.height)*i+col*n.width+row+1
				cat(name, ", ", name.of.color[name], "&#165;n")
				polygon.plot(row, col, name, name.of.color[name], height,      
                                                    width/n.width, height/n.height)
			}
		}
	}
	par(old)
}

R-help(29 Jun 2003)にhexと色の名前を変換する自作関数が紹介されていました。

  colors.hex <- function( x=colors() ) { 
          color.hex <- function(x) do.call( "rgb", as.list(col2rgb(x)/255) ) 
          sapply( x, color.hex ) 
          } 
  colors.name <- function( x ) { 
          color.name <- function( x ) colors()[ colors.hex() == x ] 
          lapply( x, color.name ) 
          } 
  colors.hex( "red" ) 
  colors.hex( colors()[1:5] ) 

  colors.name( "#FF0000" ) 
  colors.name( rainbow(3) ) 

Demo(graphic)のpieチャートにlabelをつけました。

colors.name <- function( x ) { 
          color.name <- function( x ) colors()[ colors.hex() == x ] 
          lapply( x, color.name ) 
          } 
pie(rep(1, 24), col = rainbow(24),labels=colors.name(rainbow(24)))

とすると以下のhex表示が一部色名に変わります。

colorpie.png



添付ファイル: filecolors.png 4685件 [詳細] filecolorpie.png 6128件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-03-25 (土) 11:19:16