矩形を描く rect() 関数

(グラフィックス参考実例集に戻る。Rのグラフィックスパラメータを参照する。)

矩形を描く rect() 関数は、既存のプロット領域に与えられたサイズの矩形を描く。矩形の左下隅の座標と、右上隅の座標の4つの値を与える(ベクトル化されている)。色、ハッチング、枠等を変え、必要に応じ重ね描き(後から描いたものが上描きされる)すると、意外に見栄えの良い図ができる。


例1。色、ハッチング、枠等をさまざまに変える

## set up the plot region:
op <- par(bg = "thistle")
plot(c(100, 250), c(300, 450), type = "n", xlab="", ylab="",
     main = "2 x 11 rectangles; 'rect(100+i,300+i,  150+i,380+i)'")
i <- 4*(0:10)
## draw rectangles with bottom left (100, 300)+i and top right (150, 380)+i
rect(100+i, 300+i, 150+i, 380+i, col=rainbow(11, start=.7,end=.1))
rect(240-i, 320+i, 250-i, 410+i, col=heat.colors(11), lwd=i/5)
## Background alternating  ( transparent / "bg" ) :
j <- 10*(0:5)
rect(125+j, 360+j,   141+j, 405+j/2, col = c(NA,0), border = "gold", lwd = 2)
rect(125+j, 296+j/2, 141+j, 331+j/5, col = c(NA,"midnightblue"))
mtext("+  2 x 6 rect(*, col = c(NA,0)) and  col = c(NA,\"m..blue\"))")
par(op)
 left

例2

op <- par(bg = "thistle")
## an example showing colouring and shading
plot(c(100, 200), c(300, 450), type= "n", xlab="", ylab="")
rect(100, 300, 125, 350) # transparent
rect(100, 400, 125, 450, col="green", border="blue") # coloured
rect(115, 375, 150, 425, col=par("bg"), border="transparent")
rect(150, 300, 175, 350, density=10, border="red")
rect(150, 400, 175, 450, density=30, col="blue",
     angle=-30, border="transparent")
legend(180, 450, legend=1:4, fill=c(NA, "green", par("fg"), "blue"),
       density=c(NA, NA, 10, 30), angle=c(NA, NA, 30, -30))
par(op)
 left

例3。矩形に影をつける (Ross Ihaka さんの統計数理研究所ワークショップ講演をヒント)

中間色を使うことにより見栄えの良い図が得られる。R で使える色名(Linux R 1.8.1版で687種類)の一覧は colors() で得られる。Linux ならば命令 xcolors, xcolorsel で実際の色と合わせた一覧が得られる。関数コーナーの青木さんの色表示関数も参考にしよう。

op <- par(bg="LightCyan") # 背景色指定
plot(c(0,100),c(0,100),typ="n",xlab="",ylab="") # 空の作図領域をまず描く
xl=c(0,0,0,0,0)        # 5 つの矩形の左下隅 x 座標
xr=c(30,60,45,78,59)   # 5 つの矩形の右上隅 x 座標
yb=c(10,30,50,70,90)-5 # 5 つの矩形の左下隅 y 座標
yt=yb+15               # 5 つの矩形の右上隅 y 座標
# 以下灰色の矩形を少しづつずらしながら影を描く(rect 関数はベクトル化されていることを注意)
# border 色をやはり灰色にし枠を消す
# 関数 xinch, yinch の使用法に注意(現在の作図領域に合わせて長さを変換)
for (y in (1:4)/100 ) 
  rect(xl+xinch(y), yb-yinch(y),
       xr+xinch(y), yt-yinch(y), col="grey", border="grey") 
# 最後に本来の矩形を描く(順序が大事、これまでの作図を上書き)
rect(xl,yb,xr,yt,col="orange",border="orange")
par(op) # グラフィックスオプションを復帰
 left

添付ファイル: filerect2.jpg 2832件 [詳細] fileshadebox.jpg 2817件 [詳細] filerect1.jpg 2979件 [詳細]

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