グラフィックス参考実例集:その他
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
COLOR(red){SIZE(20){グラフィックス参考実例集:その他}}
([[グラフィックス参考実例集]]に戻る。[[Rのグラフィックス...
#contents
~
-「点群の凸閉包」は、関数の先頭に、"library(tripack)"を付...
-chull は base パッケージ中にある関数です。 -- &new{2004...
-すみません。tripackはconvex.hullですね。 -- &new{2004-0...
* MASS パッケージの eqscplot 関数は x,y 軸の単位を等しく...
両軸上の等間隔なスケール(1cm)で描かれる散布図を描きます。...
#ref(グラフィックス参考実例集:その他/eqscplot.png, left)
* sunflowerplot [#me134904]
同じ位置に複数のデータがある場合、「花びら」の数でその数...
> data(iris)
> sunflowerplot(iris[, 3:4], cex = 0.2, cex.f = 1, size ...
#ref(グラフィックス参考実例集:その他/sunflowerplot.png, ...
* stars レーダーチャート、蜘蛛の巣図 [#l8fbc995]
関数starsで星形図を描くことができます。この図は、各データ...
関数starsは、内部で複数図表を用いていますから、複数図表と...
> data(mtcars)
> stars(mtcars[, 1:7], key.loc = c(14, 2), main = "Motor...
full = FALSE)
#ref(グラフィックス参考実例集:その他/stars.png, left)
* 幹葉表示 [#b2645da3]
グラフィックスというべきかどうか?度数分布表+ヒストグラ...
> data(islands) # 世界の大陸・島の面積
> stem(log10(islands)) # 常用対数値
The decimal point is at the | # | の位置に小数点がある
1 | 1111112222233444 # データ値 1.1 が6個ある
1 | 5555556666667899999
2 | 3344
2 | 59
3 | # データ 3.0,...,3.4 は無い
3 | 5678 # データ 3.5,3.6,3.7,3.8 がそ...
4 | 012 # 幹4、葉0 はアフリカの値 4.06...
* Cohen-Friendly の associaion plot。 [#f4a05359]
二元分割表の独立性からのずれを表示。(i,j) セルに対して d_...
assocplot1 <- function () {
data(HairEyeColor)
## 男女統合
x <- margin.table(HairEyeColor, c(1, 2))
assocplot(x, main = "Relation between hair and eye c...
}
#ref(グラフィックス参考実例集:その他/assocplot1.png, left)
* stripchart データ数が少ない時は箱型図よりもベター? [#s...
stripchart1 <- function () {
x <- round(rnorm(50), 1)
stripchart(x)
}
#ref(グラフィックス参考実例集:その他/stripchart1.png, le...
* dotchart [#h1e1f5da]
関数dotchartはドットチャートを描きます。これは棒グラフと...
dotchart(1:10)
引数labelsにデータのラベルを指定することもできます。
dotchart(1:10, labels=paste("sample", 1:10))
引数groupsに類別オブジェクトを指定すると、それに従って...
** 円グラフよりもベター? [#a55721dc]
つまりは棒の無い棒グラフ(下の棒グラフ版と比較せよ、よりシ...
dotchart1 <- function () {
data(VADeaths)
dotchart(VADeaths, main = "Death Rates in Virginia - 1...
}
#ref(グラフィックス参考実例集:その他/dotchart1.png, left)
** 比較用の棒グラフ [#i82fcc44]
barplot(VADeaths, beside=T, horiz=T)
#ref(グラフィックス参考実例集:その他/barplotc.png, left)
** dotchart (2) [#a0ee3a8e]
dotchart2 <- function () {
data(VADeaths)
op <- par(xaxs="i")# 0 -- 100%
dotchart(t(VADeaths), xlim = c(0,100),
main = "Death Rates in Virginia - 1940")
par(op)
}
#ref(グラフィックス参考実例集:その他/dotchart2.png, left)
*点群の凸閉包 chull [#f18b8add]
chull.ex <- function () {
old.par <- par(no.readonly = TRUE)
on.exit(par(old.par))
png("chull.png")
X <- matrix(rnorm(2000), ncol=2)
plot(X, cex=0.5)
hpts <- chull(X)
hpts <- c(hpts, hpts[1])
lines(X[hpts, ])
dev.off()
}
#ref(グラフィックス参考実例集:その他/chull.png, left)
* 自前のxy軸の例 (x軸は対数スケール、y軸は正規確率プロッ...
つまりは対数正規確率紙。このグラフ上に対数正規分布の分布...
lognormal.CDF.fn <- function(x.axis.title="Nf, Cycles"){
# 対数正規累積分布関数用グリッド
# まず、点無しでグリッドを画く
# x軸は対数尺度。y軸は確率ラベルを持つが、正規確率尺度
z.min <- -5.5
z.max <- -z.min
z.norm <- seq(z.min, z.max, length=101)
plot(NA, NA, xlim=c(3, 7), ylim=c(z.min, z.max), type="n...
yaxt="n", frame = FALSE, xlab = "", ylab = "Cumulat...
#
# x軸を対数尺度で画く
axis(side = 1, labels = FALSE, at = c(3, 4, 5, 6, 7), li...
text(x=c(3, 4, 5, 6, 7), y=rep(z.min-1.0, 5), rep("10", ...
text(x=c(3, 4, 5, 6, 7)+ 0.1, y=rep(z.min-0.8, 5)+0.08,
c("3", "4", "5", "6", "7"), xpd = NA, cex=0.8)
text(x=5.05, y=z.min-1.6, x.axis.title, xpd=NA, cex=1.)
# Draw the interior log tick marks
for (i in 3:6){
axis(side=1, at=log10(c(2, 3, 4, 5, 6, 7, 8, 9))+ i, c...
text(x=log10(c(2, 3, 4, 5, 6, 7, 8))+ i, y=rep(z.min-0.7...
c("2", "3", "4", "5", "6", "7", "8"), xpd = NA, cex...
}
#
# y軸を正規確率プロットで画く
probs <- c(1e-7, 1e-6, 1e-5, 1e-4, 0.001, 0.002, 0.005, ...
0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.98, 0.99, 0....
0.999999, 0.9999999)
z.vals <- qnorm(probs)
axis(side=2, at=z.vals , labels=NA, line = 0, tck = -0.01)
text(x=rep(3.-0.4, 4), y= -0.1+qnorm(c(1e-7, 1e-6, 1e-5...
xpd = NA, labels=rep("10", 6))
text(x=rep(3.-0.3 , 4), y= 0.1+qnorm(c(1e-7, 1e-6, 1e...
xpd = NA,labels=c("-7", "-6", "-5", "-4"))
text(x=rep(3.-0.25, 6), y= qnorm(c(0.001, 0.01, 0.1, 0....
xpd = NA,labels=c("0.001", "0.01", "0.1", "0.5", "0...
text(x=rep(3.-0.4, 4), y= -0.1-qnorm(c(1e-7, 1e-6, 1e-5...
xpd = NA, labels=rep("1-10", 6))
text(x=rep(3.-0.27 , 4), y= 0.1-qnorm(c(1e-7, 1e-6, 1...
xpd = NA,labels=c("-7", "-6", "-5", "-4"))
box()
}
#ref(グラフィックス参考実例集:その他/normalaxis.jpg,left)
* 文字列の描画 text [#re970334]
関数textで、図表領域上の任意の位置に文字列を描画すること...
text(x, y, labels)
によって、各点( x[i] , y[i] )に文字列 labels[i] を描きま...
plot(1:10)
text(locator(1), labels = "異常値")
#ref(グラフィックス参考実例集:その他/label.png, left)
* 画面の4隅に文字・式を書く mtext [#fbd2d5be]
余白に文字列を書き込むにはmtextが便利です。
mtext(text, side, line, at)
引数textに書き込む文字列を指定し、 sideには余白のサイド番...
-at には文字列を書き込む座標を指定します。この座標は、sid...
- atを省略した場合、sideで指定された余白の line行だけ図系...
mtext1 <- function () {
old.par <- par(no.readonly = TRUE); on.exit(old.par) #...
png("mtext1.png") # png デバイスを開く
plot(1:10, (-4:5)^2, main="Parabola Points", xlab="xla...
mtext("10 of them")
for(s in 1:4)
mtext(paste("mtext(..., line= -1, {side, col, font} ...
", cex = ", (1+s)/2, ")"), line = -1,
side=s, col=s, font=s, cex= (1+s)/2)
mtext("mtext(..., line= -2)", line = -2)
mtext("mtext(..., line= -2, adj = 0)", line = -2, ad...
dev.off() # デバイスを閉じる
}
#ref(グラフィックス参考実例集:その他/mtext1.png, left)
* 文字列に関する作図パラメータ [#gee4233a]
文字列描画に関する作図パラメータには以下のものがあります...
- cex :文字の大きさを、作図機器ごとに定められている標準...
- csi :cexと同じく文字の大きさを指定しますが、このパラメ...
- srt :文字列の回転角を指定します。単位は度です。x軸を基...
- crt :文字の回転角を指定します。単位は度です。x軸を基準...
- font :フォント番号を指定します。
* 長さ(inch単位)を現在のプロット領域の実際の単位に変換す...
-COLOR(magenta){説明} COLOR(red){xinch}, COLOR(red){yinch...
-COLOR(magenta){用法}
-- xinch(x = 1, warn.log = TRUE)
-- yinch(y = 1, warn.log = TRUE)
-- xyinch(xy = 1, warn.log = TRUE)
--cm(x)
-COLOR(magenta){引数}
--COLOR(red){x,y} 数値ベクトル
--COLOR(red){xy} 長さ 1 または 2 の数値
--COLOR(red){warn.log} 論理値。'TRUE' ならばもし軸が対数...
** 例1 [#hba6359b]
## プロット記号の右 0.12 インチにラベルを書く
## この例では xinch(0.12) は 0.5295534 に変換される
data(mtcars) # 組み込みデータ mtcars 読み込み
with(mtcars, { # mtcars の成分を展開した環境中で作図
plot(mpg, disp, pch=19, main= "Motor Trend Cars",
text(mpg + xinch(0.12), disp, row.names(mtcars), ad...
})
#ref(グラフィックス参考実例集:その他/xyinch.jpg, left)
** 例2 [#v4eade20]
[[矩形を描く rect() 関数]] をみよ。
* 画面を分割し、それぞれに描画。 layout 関数 [#ua472db2]
layout 関数による画面の4分割とレイアウトの確認
layout1 <- function (n) {
oldpar <- par(no.readonly = TRUE); on.exit(par(oldpar)...
# layout 関数で画面を横に 3:1、縦に1:3 の4画面に分割
# 副画面の番号を [1,1] -> 2, [1,2] -> 0, [2,1] -> 1, [...
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1)...
layout.show(nf) # レイアウトを確認したければこうする
## なぜか png デバイスは複数画面を一度に表示しないので...
pp <- recordPlot() # X11 デバイスへの出力を記録
png("layout1.png") # png デバイスを開く
replayPlot(pp) # pp に記録された画像を png デバイスへ...
dev.off() # デバイスを閉じる
}
#ref(グラフィックス参考実例集:その他/layout1.png,left)
*layout 関数の使用例。x,y データの散布図の上・右に x,y デ...
layout2 <- function (n) {
oldpar <- par(no.readonly = TRUE); on.exit(par(oldpar)...
x <- pmin(3, pmax(-3, rnorm(50))) # 50個の正規乱数. -3...
y <- pmin(3, pmax(-3, rnorm(50))) # 50個の正規乱数. -3...
xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE) # ...
yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE) # ...
top <- max(c(xhist$counts, yhist$counts))
xrange <- c(-3,3)
yrange <- c(-3,3)
# layout 関数で画面を横に 3:1、縦に1:3 の4画面に分割
# 副画面の番号を [1,1] -> 2, [1,2] -> 0, [2,1] -> 1, [...
# 番号 0 の副画面には何も描かない(?)
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1),...
# layout.show(nf) # レイアウトを確認したければこうする
par(mar=c(3,3,1,1)) #番号1の副画面の余白指定
plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="")...
par(mar=c(0,3,1,1)) #番号2の副画面の余白指定
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), spac...
par(mar=c(3,0,1,1)) #番号3の副画面の余白指定
barplot(yhist$counts, axes=FALSE, xlim=c(0, top), spac...
## なぜか png デバイスは複数画面を一度に表示しないので...
pp <- recordPlot() # X11 デバイスへの出力を記録
png("layout2.png") # png デバイスを開く
replayPlot(pp) # pp に記録された画像を png デバイスへ...
dev.off() # デバイスを閉じる
}
#ref(グラフィックス参考実例集:その他/layout2.png,left)
* スクリーン分割 split.screen [#t1260bab]
関数split.screenを使って、作図可能領域内にいくつかの異な...
adj bty cex col crt err exp font
lab las lty lwd mar mex mgp new
pch pty smo srt tck usr xaxp xaxs
xaxt xpd yaxp yaxs yaxt
なお、split.screenは内部で複数図表を利用しているので、副...
** 副画面の定義 [#w554210f]
副画面を定義するには、split.screenを使います。 引数には、...
** ベクトルによる分割 [#p5da8a1c]
split.screenにベクトル c( m , n ) を与えると、作図パラメ...
split.screen(c(2,1))
[1] 1 2
分割の結果できた各副画面は番号によって管理されます。split...
split.screen(c(1,3), screen = 2)
[1] 3 4 5
とします。
** 行列による分割 [#kc84c2ea]
split.screenに (定義する副画面の個数) × 4 の行列を与えて...
の順で与えます。副画面は互いに重なりがあっても構いません。
** (例1)。screen 関数の使用例 [#qfa0176a]
screen 関数はディスプレイを幾つかのスクリーンに分割し、そ...
screen1 <- function () {
par(bg = "white") # 既定の背景色を白にする
split.screen(c(2,1)) # ディスプレイを上下二つの...
split.screen(c(1,3), screen = 2) # スクリーン 2 (下半...
screen(1) # スクリーン 1 への書き込みを始める
plot(10:1)
screen(4) # スクリーン 4 への書き込みを始める
plot(10:1)
close.screen(all = TRUE) # スクリーン分割モードを終了
}
#ref(グラフィックス参考実例集:その他/screen1.png, left)
** (例2) スクリーン 2 がスクリーン 3,4 に分割されている。...
screen2 <- function () {
split.screen(c(2,1)) # ディスプレイを上下二つの...
split.screen(c(1,2),2) # 下のスクリーンを更に横に...
plot(1:10) # 既定で最後(screen 3)がア...
erase.screen() # ラベルを書くのを忘れたの...
plot(1:10, ylab= "ylab 3")
screen(1) # スクリーン 1 への書き込...
plot(1:10)
screen(4) # スクリーン 4 への書き込...
plot(1:10, ylab="ylab 4")
screen(1, FALSE) # スクリーン 1 へ戻るが、...
plot(10:1, axes=FALSE, lty=2, ylab="") # 上書き
axis(4) # チックマークを右軸に加える
title("Plot 1")
close.screen(all = TRUE) # スクリーン分割モードを終了
}
#ref(グラフィックス参考実例集:その他/screen2.png, left)
** (例3) 画面を 3x4 分割し、順に表示 [#ice20c44]
par(oma=c(0,0,2,0)) # 総合タイトル用に隙間を...
split.screen(figs=c(3,4)) # 画面を 3x4 分割(順に...
[1] 1 2 3 4 5 6 7 8 9 10 11 12
for(i in 1:12) {screen(i); plot(1:10)} # 各スクリーンに...
close.screen(all=T) # スクリーンへの作図を終...
title(main='TITLE 1', outer=T) # 総合タイトル
#ref(グラフィックス参考実例集:その他/mltplot.png, left)
*分割したグラフィックス画面の一部をスキップする。frame, p...
frame 関数は plot.new 関数の別名
> par(mfrow=c(2,1))
> plot.new() # 第一分割画面をスキップし、第二...
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
> par(mfrow=c(2,1))
> plot.new()
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
#ref(グラフィックス参考実例集:その他/plot.new.jpg,left)
> par(mfrow=c(2,2))
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
> plot.new()
> plot.new()
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
#ref(グラフィックス参考実例集:その他/plot.new2.jpg,left)
*高水準作図関数による図の重ね描き [#x2dc8fe0]
同じ単位のいくつかのデータを一枚の図にプロットするには...
> par(mar=c(5,4,4,4)+0.1)
> plot(1:100,type="l")
> par(new=T)
> plot(10:1, type="l", axes=F, xlab="", ylab="", lty=2)
このとき、二度めのplotによって軸と軸のラベルが重ね書きさ...
> axis(side=4)
とすることによって、現在の単位に従った、すなわち二度めのp...
> mtext(side=4, line=3, text="uwxyz")
として描きます。最後に凡例も付けてみましょう。位置はlocat...
> legend(locator(1),
legend=c("abcde", "uwxyz"), lty=1:2)
* 複数のプロットを少しずらして重ねる (r-help 記事より、20...
par(mai = c(3,1,1,1))
x <- runif(50)
plot(x, xlim = c(1, 500), ylim = c( -5, 1), bty = "l")
par(mai = c(2,1.5,2,1))
par(new = TRUE)
x <- runif(50)
plot(x, xlim = c(1, 500), ylim = c( -5, 1), bty = "l")
par(mai = c(1,2,3,1))
par(new = TRUE)
x <- runif(50)
plot(x, xlim = c(1, 500), ylim = c( -5, 1), bty = "l")
#ref(グラフィックス参考実例集:その他/overlap.png, let)
* スケールの違う y 軸を二つかく (r-help 記事より) [#v6610...
> plot(1:10) # まず最初のプロット(x 軸と y 軸(左側...
> par("usr") # その時のユーザー座標系
[1] 0.64 10.36 0.64 10.36
> par(usr=c(par("usr")[1:2], 100.8, 105.2)) # ユーザ...
> points(1:5, 105:101, col="red") # 追加の点を変更座...
> axis(4) # 変更座標系の(y)軸を右側に書く
#ref(グラフィックス参考実例集:その他/twoyaxis.png, left)
* カラーピッカー [#j11af036]
カラーチャートをクリックして色を取得
# L: division of RGB space.
# N: number of pick-up colors
colorPicker<-function(L=8,N=1){
if(L<2){cat("L must be lager than 1\n");return(NULL)}
D<-ceiling(L^(3/2)) # 3D->2D
v<-seq(0,1,by=1/(L-1));r<-rep(v,L*L);g<-rep(v,L,e=L);b...
col<-c(rgb(r,g,b),rep(rgb(1,1,1),D^2-L-3)) # construct...
x<-rep(0:(D-1),D);y<-rep(0:(D-1),each=D) # plot coord.
par.old<-par(mfrow=c(1,1))
plot(0,xlim=c(0,D),ylim=c(0,D),type="o",xaxt="n",yaxt=...
for(y in (D-1):0)for(x in 0:(D-1))
rect(x,y,x+1,y+1,col=col[1+x+(D-1-y)*D],border=NA) #...
p<-locator(N);ret<-col[1+floor(p$x)+(D-1-floor(p$y))*D...
par(par.old)
ret
}
終了行:
COLOR(red){SIZE(20){グラフィックス参考実例集:その他}}
([[グラフィックス参考実例集]]に戻る。[[Rのグラフィックス...
#contents
~
-「点群の凸閉包」は、関数の先頭に、"library(tripack)"を付...
-chull は base パッケージ中にある関数です。 -- &new{2004...
-すみません。tripackはconvex.hullですね。 -- &new{2004-0...
* MASS パッケージの eqscplot 関数は x,y 軸の単位を等しく...
両軸上の等間隔なスケール(1cm)で描かれる散布図を描きます。...
#ref(グラフィックス参考実例集:その他/eqscplot.png, left)
* sunflowerplot [#me134904]
同じ位置に複数のデータがある場合、「花びら」の数でその数...
> data(iris)
> sunflowerplot(iris[, 3:4], cex = 0.2, cex.f = 1, size ...
#ref(グラフィックス参考実例集:その他/sunflowerplot.png, ...
* stars レーダーチャート、蜘蛛の巣図 [#l8fbc995]
関数starsで星形図を描くことができます。この図は、各データ...
関数starsは、内部で複数図表を用いていますから、複数図表と...
> data(mtcars)
> stars(mtcars[, 1:7], key.loc = c(14, 2), main = "Motor...
full = FALSE)
#ref(グラフィックス参考実例集:その他/stars.png, left)
* 幹葉表示 [#b2645da3]
グラフィックスというべきかどうか?度数分布表+ヒストグラ...
> data(islands) # 世界の大陸・島の面積
> stem(log10(islands)) # 常用対数値
The decimal point is at the | # | の位置に小数点がある
1 | 1111112222233444 # データ値 1.1 が6個ある
1 | 5555556666667899999
2 | 3344
2 | 59
3 | # データ 3.0,...,3.4 は無い
3 | 5678 # データ 3.5,3.6,3.7,3.8 がそ...
4 | 012 # 幹4、葉0 はアフリカの値 4.06...
* Cohen-Friendly の associaion plot。 [#f4a05359]
二元分割表の独立性からのずれを表示。(i,j) セルに対して d_...
assocplot1 <- function () {
data(HairEyeColor)
## 男女統合
x <- margin.table(HairEyeColor, c(1, 2))
assocplot(x, main = "Relation between hair and eye c...
}
#ref(グラフィックス参考実例集:その他/assocplot1.png, left)
* stripchart データ数が少ない時は箱型図よりもベター? [#s...
stripchart1 <- function () {
x <- round(rnorm(50), 1)
stripchart(x)
}
#ref(グラフィックス参考実例集:その他/stripchart1.png, le...
* dotchart [#h1e1f5da]
関数dotchartはドットチャートを描きます。これは棒グラフと...
dotchart(1:10)
引数labelsにデータのラベルを指定することもできます。
dotchart(1:10, labels=paste("sample", 1:10))
引数groupsに類別オブジェクトを指定すると、それに従って...
** 円グラフよりもベター? [#a55721dc]
つまりは棒の無い棒グラフ(下の棒グラフ版と比較せよ、よりシ...
dotchart1 <- function () {
data(VADeaths)
dotchart(VADeaths, main = "Death Rates in Virginia - 1...
}
#ref(グラフィックス参考実例集:その他/dotchart1.png, left)
** 比較用の棒グラフ [#i82fcc44]
barplot(VADeaths, beside=T, horiz=T)
#ref(グラフィックス参考実例集:その他/barplotc.png, left)
** dotchart (2) [#a0ee3a8e]
dotchart2 <- function () {
data(VADeaths)
op <- par(xaxs="i")# 0 -- 100%
dotchart(t(VADeaths), xlim = c(0,100),
main = "Death Rates in Virginia - 1940")
par(op)
}
#ref(グラフィックス参考実例集:その他/dotchart2.png, left)
*点群の凸閉包 chull [#f18b8add]
chull.ex <- function () {
old.par <- par(no.readonly = TRUE)
on.exit(par(old.par))
png("chull.png")
X <- matrix(rnorm(2000), ncol=2)
plot(X, cex=0.5)
hpts <- chull(X)
hpts <- c(hpts, hpts[1])
lines(X[hpts, ])
dev.off()
}
#ref(グラフィックス参考実例集:その他/chull.png, left)
* 自前のxy軸の例 (x軸は対数スケール、y軸は正規確率プロッ...
つまりは対数正規確率紙。このグラフ上に対数正規分布の分布...
lognormal.CDF.fn <- function(x.axis.title="Nf, Cycles"){
# 対数正規累積分布関数用グリッド
# まず、点無しでグリッドを画く
# x軸は対数尺度。y軸は確率ラベルを持つが、正規確率尺度
z.min <- -5.5
z.max <- -z.min
z.norm <- seq(z.min, z.max, length=101)
plot(NA, NA, xlim=c(3, 7), ylim=c(z.min, z.max), type="n...
yaxt="n", frame = FALSE, xlab = "", ylab = "Cumulat...
#
# x軸を対数尺度で画く
axis(side = 1, labels = FALSE, at = c(3, 4, 5, 6, 7), li...
text(x=c(3, 4, 5, 6, 7), y=rep(z.min-1.0, 5), rep("10", ...
text(x=c(3, 4, 5, 6, 7)+ 0.1, y=rep(z.min-0.8, 5)+0.08,
c("3", "4", "5", "6", "7"), xpd = NA, cex=0.8)
text(x=5.05, y=z.min-1.6, x.axis.title, xpd=NA, cex=1.)
# Draw the interior log tick marks
for (i in 3:6){
axis(side=1, at=log10(c(2, 3, 4, 5, 6, 7, 8, 9))+ i, c...
text(x=log10(c(2, 3, 4, 5, 6, 7, 8))+ i, y=rep(z.min-0.7...
c("2", "3", "4", "5", "6", "7", "8"), xpd = NA, cex...
}
#
# y軸を正規確率プロットで画く
probs <- c(1e-7, 1e-6, 1e-5, 1e-4, 0.001, 0.002, 0.005, ...
0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.98, 0.99, 0....
0.999999, 0.9999999)
z.vals <- qnorm(probs)
axis(side=2, at=z.vals , labels=NA, line = 0, tck = -0.01)
text(x=rep(3.-0.4, 4), y= -0.1+qnorm(c(1e-7, 1e-6, 1e-5...
xpd = NA, labels=rep("10", 6))
text(x=rep(3.-0.3 , 4), y= 0.1+qnorm(c(1e-7, 1e-6, 1e...
xpd = NA,labels=c("-7", "-6", "-5", "-4"))
text(x=rep(3.-0.25, 6), y= qnorm(c(0.001, 0.01, 0.1, 0....
xpd = NA,labels=c("0.001", "0.01", "0.1", "0.5", "0...
text(x=rep(3.-0.4, 4), y= -0.1-qnorm(c(1e-7, 1e-6, 1e-5...
xpd = NA, labels=rep("1-10", 6))
text(x=rep(3.-0.27 , 4), y= 0.1-qnorm(c(1e-7, 1e-6, 1...
xpd = NA,labels=c("-7", "-6", "-5", "-4"))
box()
}
#ref(グラフィックス参考実例集:その他/normalaxis.jpg,left)
* 文字列の描画 text [#re970334]
関数textで、図表領域上の任意の位置に文字列を描画すること...
text(x, y, labels)
によって、各点( x[i] , y[i] )に文字列 labels[i] を描きま...
plot(1:10)
text(locator(1), labels = "異常値")
#ref(グラフィックス参考実例集:その他/label.png, left)
* 画面の4隅に文字・式を書く mtext [#fbd2d5be]
余白に文字列を書き込むにはmtextが便利です。
mtext(text, side, line, at)
引数textに書き込む文字列を指定し、 sideには余白のサイド番...
-at には文字列を書き込む座標を指定します。この座標は、sid...
- atを省略した場合、sideで指定された余白の line行だけ図系...
mtext1 <- function () {
old.par <- par(no.readonly = TRUE); on.exit(old.par) #...
png("mtext1.png") # png デバイスを開く
plot(1:10, (-4:5)^2, main="Parabola Points", xlab="xla...
mtext("10 of them")
for(s in 1:4)
mtext(paste("mtext(..., line= -1, {side, col, font} ...
", cex = ", (1+s)/2, ")"), line = -1,
side=s, col=s, font=s, cex= (1+s)/2)
mtext("mtext(..., line= -2)", line = -2)
mtext("mtext(..., line= -2, adj = 0)", line = -2, ad...
dev.off() # デバイスを閉じる
}
#ref(グラフィックス参考実例集:その他/mtext1.png, left)
* 文字列に関する作図パラメータ [#gee4233a]
文字列描画に関する作図パラメータには以下のものがあります...
- cex :文字の大きさを、作図機器ごとに定められている標準...
- csi :cexと同じく文字の大きさを指定しますが、このパラメ...
- srt :文字列の回転角を指定します。単位は度です。x軸を基...
- crt :文字の回転角を指定します。単位は度です。x軸を基準...
- font :フォント番号を指定します。
* 長さ(inch単位)を現在のプロット領域の実際の単位に変換す...
-COLOR(magenta){説明} COLOR(red){xinch}, COLOR(red){yinch...
-COLOR(magenta){用法}
-- xinch(x = 1, warn.log = TRUE)
-- yinch(y = 1, warn.log = TRUE)
-- xyinch(xy = 1, warn.log = TRUE)
--cm(x)
-COLOR(magenta){引数}
--COLOR(red){x,y} 数値ベクトル
--COLOR(red){xy} 長さ 1 または 2 の数値
--COLOR(red){warn.log} 論理値。'TRUE' ならばもし軸が対数...
** 例1 [#hba6359b]
## プロット記号の右 0.12 インチにラベルを書く
## この例では xinch(0.12) は 0.5295534 に変換される
data(mtcars) # 組み込みデータ mtcars 読み込み
with(mtcars, { # mtcars の成分を展開した環境中で作図
plot(mpg, disp, pch=19, main= "Motor Trend Cars",
text(mpg + xinch(0.12), disp, row.names(mtcars), ad...
})
#ref(グラフィックス参考実例集:その他/xyinch.jpg, left)
** 例2 [#v4eade20]
[[矩形を描く rect() 関数]] をみよ。
* 画面を分割し、それぞれに描画。 layout 関数 [#ua472db2]
layout 関数による画面の4分割とレイアウトの確認
layout1 <- function (n) {
oldpar <- par(no.readonly = TRUE); on.exit(par(oldpar)...
# layout 関数で画面を横に 3:1、縦に1:3 の4画面に分割
# 副画面の番号を [1,1] -> 2, [1,2] -> 0, [2,1] -> 1, [...
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1)...
layout.show(nf) # レイアウトを確認したければこうする
## なぜか png デバイスは複数画面を一度に表示しないので...
pp <- recordPlot() # X11 デバイスへの出力を記録
png("layout1.png") # png デバイスを開く
replayPlot(pp) # pp に記録された画像を png デバイスへ...
dev.off() # デバイスを閉じる
}
#ref(グラフィックス参考実例集:その他/layout1.png,left)
*layout 関数の使用例。x,y データの散布図の上・右に x,y デ...
layout2 <- function (n) {
oldpar <- par(no.readonly = TRUE); on.exit(par(oldpar)...
x <- pmin(3, pmax(-3, rnorm(50))) # 50個の正規乱数. -3...
y <- pmin(3, pmax(-3, rnorm(50))) # 50個の正規乱数. -3...
xhist <- hist(x, breaks=seq(-3,3,0.5), plot=FALSE) # ...
yhist <- hist(y, breaks=seq(-3,3,0.5), plot=FALSE) # ...
top <- max(c(xhist$counts, yhist$counts))
xrange <- c(-3,3)
yrange <- c(-3,3)
# layout 関数で画面を横に 3:1、縦に1:3 の4画面に分割
# 副画面の番号を [1,1] -> 2, [1,2] -> 0, [2,1] -> 1, [...
# 番号 0 の副画面には何も描かない(?)
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1),...
# layout.show(nf) # レイアウトを確認したければこうする
par(mar=c(3,3,1,1)) #番号1の副画面の余白指定
plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="")...
par(mar=c(0,3,1,1)) #番号2の副画面の余白指定
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), spac...
par(mar=c(3,0,1,1)) #番号3の副画面の余白指定
barplot(yhist$counts, axes=FALSE, xlim=c(0, top), spac...
## なぜか png デバイスは複数画面を一度に表示しないので...
pp <- recordPlot() # X11 デバイスへの出力を記録
png("layout2.png") # png デバイスを開く
replayPlot(pp) # pp に記録された画像を png デバイスへ...
dev.off() # デバイスを閉じる
}
#ref(グラフィックス参考実例集:その他/layout2.png,left)
* スクリーン分割 split.screen [#t1260bab]
関数split.screenを使って、作図可能領域内にいくつかの異な...
adj bty cex col crt err exp font
lab las lty lwd mar mex mgp new
pch pty smo srt tck usr xaxp xaxs
xaxt xpd yaxp yaxs yaxt
なお、split.screenは内部で複数図表を利用しているので、副...
** 副画面の定義 [#w554210f]
副画面を定義するには、split.screenを使います。 引数には、...
** ベクトルによる分割 [#p5da8a1c]
split.screenにベクトル c( m , n ) を与えると、作図パラメ...
split.screen(c(2,1))
[1] 1 2
分割の結果できた各副画面は番号によって管理されます。split...
split.screen(c(1,3), screen = 2)
[1] 3 4 5
とします。
** 行列による分割 [#kc84c2ea]
split.screenに (定義する副画面の個数) × 4 の行列を与えて...
の順で与えます。副画面は互いに重なりがあっても構いません。
** (例1)。screen 関数の使用例 [#qfa0176a]
screen 関数はディスプレイを幾つかのスクリーンに分割し、そ...
screen1 <- function () {
par(bg = "white") # 既定の背景色を白にする
split.screen(c(2,1)) # ディスプレイを上下二つの...
split.screen(c(1,3), screen = 2) # スクリーン 2 (下半...
screen(1) # スクリーン 1 への書き込みを始める
plot(10:1)
screen(4) # スクリーン 4 への書き込みを始める
plot(10:1)
close.screen(all = TRUE) # スクリーン分割モードを終了
}
#ref(グラフィックス参考実例集:その他/screen1.png, left)
** (例2) スクリーン 2 がスクリーン 3,4 に分割されている。...
screen2 <- function () {
split.screen(c(2,1)) # ディスプレイを上下二つの...
split.screen(c(1,2),2) # 下のスクリーンを更に横に...
plot(1:10) # 既定で最後(screen 3)がア...
erase.screen() # ラベルを書くのを忘れたの...
plot(1:10, ylab= "ylab 3")
screen(1) # スクリーン 1 への書き込...
plot(1:10)
screen(4) # スクリーン 4 への書き込...
plot(1:10, ylab="ylab 4")
screen(1, FALSE) # スクリーン 1 へ戻るが、...
plot(10:1, axes=FALSE, lty=2, ylab="") # 上書き
axis(4) # チックマークを右軸に加える
title("Plot 1")
close.screen(all = TRUE) # スクリーン分割モードを終了
}
#ref(グラフィックス参考実例集:その他/screen2.png, left)
** (例3) 画面を 3x4 分割し、順に表示 [#ice20c44]
par(oma=c(0,0,2,0)) # 総合タイトル用に隙間を...
split.screen(figs=c(3,4)) # 画面を 3x4 分割(順に...
[1] 1 2 3 4 5 6 7 8 9 10 11 12
for(i in 1:12) {screen(i); plot(1:10)} # 各スクリーンに...
close.screen(all=T) # スクリーンへの作図を終...
title(main='TITLE 1', outer=T) # 総合タイトル
#ref(グラフィックス参考実例集:その他/mltplot.png, left)
*分割したグラフィックス画面の一部をスキップする。frame, p...
frame 関数は plot.new 関数の別名
> par(mfrow=c(2,1))
> plot.new() # 第一分割画面をスキップし、第二...
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
> par(mfrow=c(2,1))
> plot.new()
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
#ref(グラフィックス参考実例集:その他/plot.new.jpg,left)
> par(mfrow=c(2,2))
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
> plot.new()
> plot.new()
> hist(rgamma(100000,6463.7,scale=0.015471),xlim=c(0,120...
#ref(グラフィックス参考実例集:その他/plot.new2.jpg,left)
*高水準作図関数による図の重ね描き [#x2dc8fe0]
同じ単位のいくつかのデータを一枚の図にプロットするには...
> par(mar=c(5,4,4,4)+0.1)
> plot(1:100,type="l")
> par(new=T)
> plot(10:1, type="l", axes=F, xlab="", ylab="", lty=2)
このとき、二度めのplotによって軸と軸のラベルが重ね書きさ...
> axis(side=4)
とすることによって、現在の単位に従った、すなわち二度めのp...
> mtext(side=4, line=3, text="uwxyz")
として描きます。最後に凡例も付けてみましょう。位置はlocat...
> legend(locator(1),
legend=c("abcde", "uwxyz"), lty=1:2)
* 複数のプロットを少しずらして重ねる (r-help 記事より、20...
par(mai = c(3,1,1,1))
x <- runif(50)
plot(x, xlim = c(1, 500), ylim = c( -5, 1), bty = "l")
par(mai = c(2,1.5,2,1))
par(new = TRUE)
x <- runif(50)
plot(x, xlim = c(1, 500), ylim = c( -5, 1), bty = "l")
par(mai = c(1,2,3,1))
par(new = TRUE)
x <- runif(50)
plot(x, xlim = c(1, 500), ylim = c( -5, 1), bty = "l")
#ref(グラフィックス参考実例集:その他/overlap.png, let)
* スケールの違う y 軸を二つかく (r-help 記事より) [#v6610...
> plot(1:10) # まず最初のプロット(x 軸と y 軸(左側...
> par("usr") # その時のユーザー座標系
[1] 0.64 10.36 0.64 10.36
> par(usr=c(par("usr")[1:2], 100.8, 105.2)) # ユーザ...
> points(1:5, 105:101, col="red") # 追加の点を変更座...
> axis(4) # 変更座標系の(y)軸を右側に書く
#ref(グラフィックス参考実例集:その他/twoyaxis.png, left)
* カラーピッカー [#j11af036]
カラーチャートをクリックして色を取得
# L: division of RGB space.
# N: number of pick-up colors
colorPicker<-function(L=8,N=1){
if(L<2){cat("L must be lager than 1\n");return(NULL)}
D<-ceiling(L^(3/2)) # 3D->2D
v<-seq(0,1,by=1/(L-1));r<-rep(v,L*L);g<-rep(v,L,e=L);b...
col<-c(rgb(r,g,b),rep(rgb(1,1,1),D^2-L-3)) # construct...
x<-rep(0:(D-1),D);y<-rep(0:(D-1),each=D) # plot coord.
par.old<-par(mfrow=c(1,1))
plot(0,xlim=c(0,D),ylim=c(0,D),type="o",xaxt="n",yaxt=...
for(y in (D-1):0)for(x in 0:(D-1))
rect(x,y,x+1,y+1,col=col[1+x+(D-1-y)*D],border=NA) #...
p<-locator(N);ret<-col[1+floor(p$x)+(D-1-floor(p$y))*D...
par(par.old)
ret
}
ページ名: