グラフィックス参考実例集:曲線のグラフ
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
COLOR(red){SIZE(20){グラフィックス参考実例集:曲線のグラ...
([[グラフィックス参考実例集]]に戻る。[[Rのグラフィックス...
曲線のグラフを書く基本は plot 関数と curve 関数です。前者...
#contents
~
*curve [#g18ae937]
引数に与えられた関数か x による expression に対応する曲線...
curve 関数には曲線方程式、x の範囲と分割数を与えます。
curve1 <- function () {
old.par <- par(no.readonly = TRUE)); on.exit(old.par)
png("curve1.png")
par(mfrow=c(2,2)) # 画面を 2x2 分割
curve(x^3-3*x, -2, 2) # 関数を直接与える
curve(x^2-2, add = TRUE, col = "violet") # 重ねる
plot(cos, xlim = c(-pi,3*pi), n = 1001, col = "blue") ...
chippy <- function(x) sin(cos(x)*exp(-x/2)) # 複雑な関...
curve(chippy, -8, 7, n=2001)
curve(chippy, -8, -5)
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/curve1.png, l...
** 対数軸の利用 [#f57d21d3]
curve2 <- function () {
old.par <- par(no.readonly = TRUE); on.exit(old.par)
par(mfrow=c(2,2)) # 画面を 2x2 分割
for(ll in c("","x","y","xy"))
curve(log(1+x), 1,100, log=ll, sub=paste("log= '",ll...
}
#ref(グラフィックス参考実例集:曲線のグラフ/curve2.png, l...
*正規分布のグラフ [#fb66f603]
**準備(線分と矢印) [#u6935a36]
segmentsはいくつかの線分を一度に描きます。引数には、各線...
segments(x1, y1, x2, y2)
という形で指定します。
**準備(矢印) [#q7a7802e]
また、関数arrowsはsegmentsと同じ引数をとり、始点の座標(x1...
**準備(マーカの描画) [#t7b39c0a]
関数pointsは点の座標を指定してマーカを描きます。引数に...
**準備(文字列の描画) [#xef39bb5]
関数textで、図表領域上の任意の位置に文字列を描画するこ...
text(x, y, labels)
によって、各点( x[i] , y[i] )に文字列 labels[i] を描きま...
mtext(text, side, line, at)
引数textに書き込む文字列を指定し、 sideには余白のサイド番...
**準備(文字列に関する作図パラメータ) [#uf9d4c07]
文字列描画に関する作図パラメータには以下のものがあります...
-cex 文字の大きさを、作図機器ごとに定められている標準文...
-csi cexと同じく文字の大きさを指定しますが、このパラメ...
-srt 文字列の回転角を指定します。単位は度です。x軸を基...
-crt 文字の回転角を指定します。単位は度です。x軸を基準...
-adj 文字列の先頭を0、最後を1とした時に、指定位置が文字...
-font フォント番号を指定します。
**正規分布のグラフ [#p2f6d112]
normgraph1 <- function (k) {
old.par <- par(no.readonly = TRUE); on.exit(par(old.pa...
par(lab=c(8,8,0))
png("normalgraph1.png")
plot(dnorm, -4, 4, xlab="", ylab="density", main="Norm...
abline(h=0)
segments(-3, 0, -3, 0.148); segments(3, 0...
segments(-2, 0, -2, dnorm(-2)); segments(2, 0...
segments(-1, 0, -1, dnorm(-1)); segments(1, 0...
arrows(-2, dnorm(-1), -1, dnorm(-1)); arrows(2, dnorm...
arrows(-1.1, dnorm(-2), -2, dnorm(-2)); arrows(1.1, d...
arrows(-1.1, 0.148, -3, 0.148); arrows(1.1, ...
text(0, dnorm(1), labels="68.26%")
text(0, dnorm(2), labels="95.45%")
text(0, (dnorm(1)+dnorm(2))/2, labels="99.73%")
par(ps=15)
text(-3, 0, expression(mu-3*sigma), pos=1, offset=0.5,...
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/normalgraph1....
**正規分布のグラフ (平均と分散を変えた数種類のグラフを同...
normgraph2 <- function () {
op <- par(no.readonly = TRUE); on.exit(par(old.par))
par(usr=c(-5,5,0,2)) # ユーザー座標指定
mh <- dnorm(0,0,0.7) # 最大 y 座標値
png("normalgraph2.png")
normal1 <- function(x) dnorm(x, mean=0, sd=1) # 正規...
plot(normal1, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal2 <- function(x) dnorm(x, mean=2, sd=1) # 正規...
par(new=TRUE)
plot(normal2, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal3 <- function(x) dnorm(x, mean=0, sd=2) # 正規...
par(new=TRUE)
plot(normal3, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal4 <- function(x) dnorm(x, mean=0, sd=0.7) # 正規...
par(new=TRUE)
plot(normal4, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal5 <- function(x) dnorm(x, mean=-3, sd=0.9) # 正...
par(new=TRUE)
plot(normal5, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
dev.off()
}
#ref(normalgraph2.png)
*第59回 Toeic Reading Score への正規分布の当てはめ [#v47b...
公表平均 262.1, 標準偏差 89.3, 受検者 40,944名。
toeic.r <- function () {
old.par <- par(no.readonly = TRUE); on.exit(par(old.pa...
dt <- c(71,190,490,1251,1666,2404,3898,3501,4678,3702,
3687,4153,2920,2507,1440,1508,1197,500,181)
cs <- c(5,45,70,95,120,145,170,195,220,245,270,295,
320,345,370,395,420,445,470,500)
ccs <- cs[-1]-cs[-length(cs)]
ft <- function(x) {dnorm(x, mean = 262.1, sd = 89.3)}
par(usr=c(0,500,0,0.005))
png("toeic.reading.png")
ddt <- dt/sum(dt)/ccs
barplot(ddt, width = ccs, space = 0, col = 'grey' # 棒...
, xlim = c(0,500), ylim = c(0,0.005), xpd = TRUE
, axes = TRUE)
# curve 関数使用。101個のサンプル点間を直線で結ぶ
curve(ft, from = 0, to = 500, n = 101, add = TRUE, typ...
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/toeic.reading...
*F 分布のグラフ(polygon 関数による一部塗りつぶし) [#w29ae...
**準備(polygon関数) [#v9629f7d]
polygonは、多角形を描き、その中を塗りつぶす(または斜線で...
polygon(x, y)
引数xとyには多角形の頂点のx、y座標のベクトルを指定します...
引数densityによって、多角形の内部を塗りつぶすか、ハッチン...
引数colに色番号を指定することによって、輪郭線と塗りつぶし...
border=Fを指定すると、多角形の輪郭を描きません。
**F 分布のグラフ [#v4542624]
Fgraph <- function () {
old.par <- par(no.readonly = TRUE); on.exit(par(old.pa...
par(lab=c(8,8,0))
png("Fgraph1.png")
DF <- function(x) df(x, df1=9, df2=9) # F 分布の密度関数
plot(DF, 0, 6, xlab="", ylab="density", main="F distri...
abline(h=0)
## 領域をx軸方向に50個の多角形(台形)に等分割
xvals <- seq(qf(0.025,df1=9,df2=9),qf(0.975,df1=9,df2=...
dvals <- DF(xvals) # 対応するグラフの高さ
## 多角形塗り潰し
polygon(c(xvals,rev(xvals)),c(rep(0,50),rev(dvals)),co...
arrows(0.7983, DF(0.7983), 0.7983, 0, length=0.2)
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/Fgraph.png,le...
* 確率分布のグラフの幾つかの例 (三中信宏さんウェッブペー...
[[三中信宏さんウェッブページ:http://cse.niaes.affrc.go.jp...
* lines 関数を用いた曲線の例 [#wc057b8c]
**折れ線 [#s3686e1d]
linesは指定された点を通る折れ線を描きます。引数には、plot...
**直線 [#f7b3e39a]
ablineは切片、傾きを指定して、直線を描く関数です。使い...
-abline(a, b) a、bは切片と傾き。直線 y = a + bx を描き...
-abline(c(a, b)) 上と同じく直線 y = a + bx を描きます。
-abline(h=y) yはy座標のベクトル。与えられたy座標の水平...
-abline(v=x) xはx座標のベクトル。与えられた x 座標の垂...
-abline(reg) regはcoef要素を持つリストです。典型的にはl...
ablineを使って、図に格子を入れることができます。
> plot(1:10)
> abline(a=0, b=1) # 切片0 傾き1の直線
> abline(h=1:10, v=1:10, lty=2) # 格子を描く
**lines 関数を用いた曲線の例 [#v21102d8]
plot 関数のオプション type='n' で枠だけをまず描き、そこへ...
bessel1 <- function() {
nus <- c(0:5,10,20) # Bessel 関数の次数
x0 <- 2^(-20:10) # サンプリング x 座標ベクトル
## 注意:両対数軸枠とタイトルだけをまず用意( type='n'...
plot(x0, x0^-8, log='xy', ylab="",type='n',
main = "Bessel Functions J_nu(x) near 0\n log - ...
## 点の間を線で結んで曲線を書く(lines 関数は直前の領...
for(nu in sort(c(nus,nus+.5))) lines(x0,besselJ(x0,nu...
legend(3, 1e50, leg=paste("nu=", paste(nus, nus+.5, s...
}
枠だけだとこんな風(これ以降のプロットはこの座標に対して行...
#ref(グラフィックス参考実例集:曲線のグラフ/bes1.png, left)
完成
#ref(グラフィックス参考実例集:曲線のグラフ/bes11.png, le...
* matplot 関数で一度に複数の曲線を描く [#z0498d5e]
expression 関数による数学記号の作図にも注意
bessel2 <- function() {
op <- par(no.readonly = TRUE); on.exit(par(old.par))
xx <- 2:7
nu <- seq(-10,9, len = 2001)
par(lab = c(16,5,7))
## outer(xx, nu, besselI) は i,j 成分が besselI(xx[i]...
matplot(nu, t(outer(xx, nu, besselI)), type = 'l', yl...
main = expression(paste("Bessel ", I[nu](x), " ...
", as ", f(nu))), xlab = expression(nu))
abline(v=0, col = "light gray", lty = 3)
legend(5, 200, leg = paste("x=", xx, " "), col=seq(x...
}
#ref(グラフィックス参考実例集:曲線のグラフ/bes2.png, left)
* plot 関数の点の色を変数に応じて変える (r-help 記事より ...
x <- runif(40); y <- runif(40); z <- runif(40)
plot(x, y, col = ifelse(z>0.5, "red", "blue")) # z>0....
#ref(グラフィックス参考実例集:曲線のグラフ/plotcolor.png...
* プロットを線で結ぶ際、欠損値部分には線を引かない -- 200...
折れ線プロット plot(..., type='l') は欠損値 NA がある点で...
> x <- seq(0,4*pi,by=0.1)
> y <- sin(x)
> y[abs(y)>=0.5]=NA # abs(y)>=0.5 なら値を NA としておく
> plot(x,y,type='l') # abs(y) < 0.5 の部分だけをプロット
#ref(グラフィックス参考実例集:曲線のグラフ/missingplot.p...
終了行:
COLOR(red){SIZE(20){グラフィックス参考実例集:曲線のグラ...
([[グラフィックス参考実例集]]に戻る。[[Rのグラフィックス...
曲線のグラフを書く基本は plot 関数と curve 関数です。前者...
#contents
~
*curve [#g18ae937]
引数に与えられた関数か x による expression に対応する曲線...
curve 関数には曲線方程式、x の範囲と分割数を与えます。
curve1 <- function () {
old.par <- par(no.readonly = TRUE)); on.exit(old.par)
png("curve1.png")
par(mfrow=c(2,2)) # 画面を 2x2 分割
curve(x^3-3*x, -2, 2) # 関数を直接与える
curve(x^2-2, add = TRUE, col = "violet") # 重ねる
plot(cos, xlim = c(-pi,3*pi), n = 1001, col = "blue") ...
chippy <- function(x) sin(cos(x)*exp(-x/2)) # 複雑な関...
curve(chippy, -8, 7, n=2001)
curve(chippy, -8, -5)
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/curve1.png, l...
** 対数軸の利用 [#f57d21d3]
curve2 <- function () {
old.par <- par(no.readonly = TRUE); on.exit(old.par)
par(mfrow=c(2,2)) # 画面を 2x2 分割
for(ll in c("","x","y","xy"))
curve(log(1+x), 1,100, log=ll, sub=paste("log= '",ll...
}
#ref(グラフィックス参考実例集:曲線のグラフ/curve2.png, l...
*正規分布のグラフ [#fb66f603]
**準備(線分と矢印) [#u6935a36]
segmentsはいくつかの線分を一度に描きます。引数には、各線...
segments(x1, y1, x2, y2)
という形で指定します。
**準備(矢印) [#q7a7802e]
また、関数arrowsはsegmentsと同じ引数をとり、始点の座標(x1...
**準備(マーカの描画) [#t7b39c0a]
関数pointsは点の座標を指定してマーカを描きます。引数に...
**準備(文字列の描画) [#xef39bb5]
関数textで、図表領域上の任意の位置に文字列を描画するこ...
text(x, y, labels)
によって、各点( x[i] , y[i] )に文字列 labels[i] を描きま...
mtext(text, side, line, at)
引数textに書き込む文字列を指定し、 sideには余白のサイド番...
**準備(文字列に関する作図パラメータ) [#uf9d4c07]
文字列描画に関する作図パラメータには以下のものがあります...
-cex 文字の大きさを、作図機器ごとに定められている標準文...
-csi cexと同じく文字の大きさを指定しますが、このパラメ...
-srt 文字列の回転角を指定します。単位は度です。x軸を基...
-crt 文字の回転角を指定します。単位は度です。x軸を基準...
-adj 文字列の先頭を0、最後を1とした時に、指定位置が文字...
-font フォント番号を指定します。
**正規分布のグラフ [#p2f6d112]
normgraph1 <- function (k) {
old.par <- par(no.readonly = TRUE); on.exit(par(old.pa...
par(lab=c(8,8,0))
png("normalgraph1.png")
plot(dnorm, -4, 4, xlab="", ylab="density", main="Norm...
abline(h=0)
segments(-3, 0, -3, 0.148); segments(3, 0...
segments(-2, 0, -2, dnorm(-2)); segments(2, 0...
segments(-1, 0, -1, dnorm(-1)); segments(1, 0...
arrows(-2, dnorm(-1), -1, dnorm(-1)); arrows(2, dnorm...
arrows(-1.1, dnorm(-2), -2, dnorm(-2)); arrows(1.1, d...
arrows(-1.1, 0.148, -3, 0.148); arrows(1.1, ...
text(0, dnorm(1), labels="68.26%")
text(0, dnorm(2), labels="95.45%")
text(0, (dnorm(1)+dnorm(2))/2, labels="99.73%")
par(ps=15)
text(-3, 0, expression(mu-3*sigma), pos=1, offset=0.5,...
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/normalgraph1....
**正規分布のグラフ (平均と分散を変えた数種類のグラフを同...
normgraph2 <- function () {
op <- par(no.readonly = TRUE); on.exit(par(old.par))
par(usr=c(-5,5,0,2)) # ユーザー座標指定
mh <- dnorm(0,0,0.7) # 最大 y 座標値
png("normalgraph2.png")
normal1 <- function(x) dnorm(x, mean=0, sd=1) # 正規...
plot(normal1, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal2 <- function(x) dnorm(x, mean=2, sd=1) # 正規...
par(new=TRUE)
plot(normal2, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal3 <- function(x) dnorm(x, mean=0, sd=2) # 正規...
par(new=TRUE)
plot(normal3, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal4 <- function(x) dnorm(x, mean=0, sd=0.7) # 正規...
par(new=TRUE)
plot(normal4, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
normal5 <- function(x) dnorm(x, mean=-3, sd=0.9) # 正...
par(new=TRUE)
plot(normal5, -5, 5, xlim=c(-5,5), ylim=c(0,mh), xlab="...
dev.off()
}
#ref(normalgraph2.png)
*第59回 Toeic Reading Score への正規分布の当てはめ [#v47b...
公表平均 262.1, 標準偏差 89.3, 受検者 40,944名。
toeic.r <- function () {
old.par <- par(no.readonly = TRUE); on.exit(par(old.pa...
dt <- c(71,190,490,1251,1666,2404,3898,3501,4678,3702,
3687,4153,2920,2507,1440,1508,1197,500,181)
cs <- c(5,45,70,95,120,145,170,195,220,245,270,295,
320,345,370,395,420,445,470,500)
ccs <- cs[-1]-cs[-length(cs)]
ft <- function(x) {dnorm(x, mean = 262.1, sd = 89.3)}
par(usr=c(0,500,0,0.005))
png("toeic.reading.png")
ddt <- dt/sum(dt)/ccs
barplot(ddt, width = ccs, space = 0, col = 'grey' # 棒...
, xlim = c(0,500), ylim = c(0,0.005), xpd = TRUE
, axes = TRUE)
# curve 関数使用。101個のサンプル点間を直線で結ぶ
curve(ft, from = 0, to = 500, n = 101, add = TRUE, typ...
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/toeic.reading...
*F 分布のグラフ(polygon 関数による一部塗りつぶし) [#w29ae...
**準備(polygon関数) [#v9629f7d]
polygonは、多角形を描き、その中を塗りつぶす(または斜線で...
polygon(x, y)
引数xとyには多角形の頂点のx、y座標のベクトルを指定します...
引数densityによって、多角形の内部を塗りつぶすか、ハッチン...
引数colに色番号を指定することによって、輪郭線と塗りつぶし...
border=Fを指定すると、多角形の輪郭を描きません。
**F 分布のグラフ [#v4542624]
Fgraph <- function () {
old.par <- par(no.readonly = TRUE); on.exit(par(old.pa...
par(lab=c(8,8,0))
png("Fgraph1.png")
DF <- function(x) df(x, df1=9, df2=9) # F 分布の密度関数
plot(DF, 0, 6, xlab="", ylab="density", main="F distri...
abline(h=0)
## 領域をx軸方向に50個の多角形(台形)に等分割
xvals <- seq(qf(0.025,df1=9,df2=9),qf(0.975,df1=9,df2=...
dvals <- DF(xvals) # 対応するグラフの高さ
## 多角形塗り潰し
polygon(c(xvals,rev(xvals)),c(rep(0,50),rev(dvals)),co...
arrows(0.7983, DF(0.7983), 0.7983, 0, length=0.2)
dev.off()
}
#ref(グラフィックス参考実例集:曲線のグラフ/Fgraph.png,le...
* 確率分布のグラフの幾つかの例 (三中信宏さんウェッブペー...
[[三中信宏さんウェッブページ:http://cse.niaes.affrc.go.jp...
* lines 関数を用いた曲線の例 [#wc057b8c]
**折れ線 [#s3686e1d]
linesは指定された点を通る折れ線を描きます。引数には、plot...
**直線 [#f7b3e39a]
ablineは切片、傾きを指定して、直線を描く関数です。使い...
-abline(a, b) a、bは切片と傾き。直線 y = a + bx を描き...
-abline(c(a, b)) 上と同じく直線 y = a + bx を描きます。
-abline(h=y) yはy座標のベクトル。与えられたy座標の水平...
-abline(v=x) xはx座標のベクトル。与えられた x 座標の垂...
-abline(reg) regはcoef要素を持つリストです。典型的にはl...
ablineを使って、図に格子を入れることができます。
> plot(1:10)
> abline(a=0, b=1) # 切片0 傾き1の直線
> abline(h=1:10, v=1:10, lty=2) # 格子を描く
**lines 関数を用いた曲線の例 [#v21102d8]
plot 関数のオプション type='n' で枠だけをまず描き、そこへ...
bessel1 <- function() {
nus <- c(0:5,10,20) # Bessel 関数の次数
x0 <- 2^(-20:10) # サンプリング x 座標ベクトル
## 注意:両対数軸枠とタイトルだけをまず用意( type='n'...
plot(x0, x0^-8, log='xy', ylab="",type='n',
main = "Bessel Functions J_nu(x) near 0\n log - ...
## 点の間を線で結んで曲線を書く(lines 関数は直前の領...
for(nu in sort(c(nus,nus+.5))) lines(x0,besselJ(x0,nu...
legend(3, 1e50, leg=paste("nu=", paste(nus, nus+.5, s...
}
枠だけだとこんな風(これ以降のプロットはこの座標に対して行...
#ref(グラフィックス参考実例集:曲線のグラフ/bes1.png, left)
完成
#ref(グラフィックス参考実例集:曲線のグラフ/bes11.png, le...
* matplot 関数で一度に複数の曲線を描く [#z0498d5e]
expression 関数による数学記号の作図にも注意
bessel2 <- function() {
op <- par(no.readonly = TRUE); on.exit(par(old.par))
xx <- 2:7
nu <- seq(-10,9, len = 2001)
par(lab = c(16,5,7))
## outer(xx, nu, besselI) は i,j 成分が besselI(xx[i]...
matplot(nu, t(outer(xx, nu, besselI)), type = 'l', yl...
main = expression(paste("Bessel ", I[nu](x), " ...
", as ", f(nu))), xlab = expression(nu))
abline(v=0, col = "light gray", lty = 3)
legend(5, 200, leg = paste("x=", xx, " "), col=seq(x...
}
#ref(グラフィックス参考実例集:曲線のグラフ/bes2.png, left)
* plot 関数の点の色を変数に応じて変える (r-help 記事より ...
x <- runif(40); y <- runif(40); z <- runif(40)
plot(x, y, col = ifelse(z>0.5, "red", "blue")) # z>0....
#ref(グラフィックス参考実例集:曲線のグラフ/plotcolor.png...
* プロットを線で結ぶ際、欠損値部分には線を引かない -- 200...
折れ線プロット plot(..., type='l') は欠損値 NA がある点で...
> x <- seq(0,4*pi,by=0.1)
> y <- sin(x)
> y[abs(y)>=0.5]=NA # abs(y)>=0.5 なら値を NA としておく
> plot(x,y,type='l') # abs(y) < 0.5 の部分だけをプロット
#ref(グラフィックス参考実例集:曲線のグラフ/missingplot.p...
ページ名: