グラフィックス参考実例集:棒グラフ
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
COLOR(red){SIZE(20){グラフィックス参考実例集:棒グラフ、...
([[グラフィックス参考実例集]]に戻る。[[Rのグラフィックス...
#contents
~
*単純な棒グラフ [#d2840426]
barplot 関数は棒グラフを描きます。棒グラフは、棒の長さに...
引数が行列ならば各列が一本の棒となり、列の各要素の値によ...
-引数angle、density、colはそれぞれ棒を塗り分ける線分の角...
-引数legendを指定すると、右上に凡例を描きます。この引数に...
-引数namesには各棒のラベルを定める文字型ベクトル指定する...
-引数widthは各棒の相対的な幅を定めるベクトルです。spaceの...
-引数besideをTRUEにすると、引数に与えられた行列の列毎でブ...
-引数horizをTRUEにすると棒を水平にします。この場合データ...
-barplotは各棒の中心位置のx座標を返します。この値とtextな...
似たものにヒストグラムを描く hist 関数がある。見かけは似...
注意。base パッケージ中の barplot 関数 (hist も?) は対数...
** とりあえず書いてみる [#idc0114b]
barplot01 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot01.png")
d <- ceiling(runif(5, 1, 10)) # サンプルデータ
barplot(d)
}
barplot01()
#ref(r_case03_barplot01.png,left)
** x軸に名前を表示して、棒の色を変更する [#ga16adb5]
barplot03 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot03.png")
d <- ceiling(runif(5, 1, 10))
d.names <- paste("gene", 1:5) # x軸に表示される名前...
barplot(d, names.arg=d.names, col=c("blue", "green", "...
}
barplot03()
#ref(r_case03_barplot03.png,left)
** 誤差範囲を表示する [#oae18ac7]
棒グラフの棒の幅は1になっています(barplotのwidth引数)。棒...
arrowsのangleは矢印の傾きを指定します。今回は直角になるよ...
barplot04 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot04.png")
d <- ceiling(runif(5, 1, 10))
d.names <- paste("gene", 1:5)
d.error <- runif(5, 0, 1) # 誤差範囲のデータ
d.error.x <- (0.2 + 1) * 1:5 - 1/2 # エラーバーを表...
barplot(d, names.arg=d.names, col=c("blue", "green", "...
arrows(d.error.x, d, d.error.x, d + d.error, angle=90)
arrows(d.error.x, d, d.error.x, d - d.error, angle=90)
}
barplot04()
#ref(r_case03_barplot04.png,left)
別の方法:関数barplotは各棒グラフの中点座標の配列を返すの...
barplot04b <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot04b.png")
d <- ceiling(runif(5, 1, 10))
d.names <- paste("gene", 1:5)
d.error <- runif(5, 0, 1) # 誤差範囲のデータ
# 各棒グラフの中点座標を取得
d.error.x <- barplot(d, names.arg=d.names, col=c("blue...
arrows(d.error.x, d, d.error.x, d + d.error, angle=90)
arrows(d.error.x, d, d.error.x, d - d.error, angle=90)
}
barplot04b()
*積み重ねの棒グラフ [#re2e5983]
** まずは書いてみる [#y574f5e1]
barplot05 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot05.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3) # サン...
barplot(dm)
}
barplot05()
#ref(r_case03_barplot05.png,left)
** 棒グラフの間に線を引く [#i816a9db]
棒グラフの間の線はsegments関数で引きます。segmentsには線...
barplot06 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot06.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.seg.xs <- rep((0.2 + 1) * 1:(ncol(dm) - 1), each=nro...
d.seg.xe <- rep((0.2 + 1) * 2:ncol(dm) - 1, each=nrow(...
d.seg.ys <- apply(dm[,1:(ncol(dm) - 1)], 2, cumsum) ...
d.seg.ye <- apply(dm[,2:ncol(dm)], 2, cumsum) ...
barplot(dm)
segments(d.seg.xs, d.seg.ys, d.seg.xe, d.seg.ye) # ...
}
barplot06()
#ref(r_case03_barplot06.png,left)
** 凡例を表示する [#kac63249]
barplot07 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot07.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.seg.xs <- rep((0.2 + 1) * 1:(ncol(dm) - 1), each=nro...
d.seg.xe <- rep((0.2 + 1) * 2:ncol(dm) - 1, each=nrow(...
d.seg.ys <- apply(dm[,1:(ncol(dm) - 1)], 2, cumsum)
d.seg.ye <- apply(dm[,2:ncol(dm)], 2, cumsum)
d.names.r <- paste("exp", 1:nrow(dm)) # 凡例に表示...
d.names.c<-paste("gene",1:ncol(dm)) # 棒グラフのグル...
barplot(dm, names.arg=d.names.c, legend.text=d.names.r)
segments(d.seg.xs, d.seg.ys, d.seg.xe, d.seg.ye)
}
barplot07()
#ref(r_case03_barplot07.png,left)
*横に並べた棒グラフ [#m9d8e0d3]
** まずは書いてみる [#idf69853]
barplot08 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot08.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3) # サン...
barplot(dm, beside=T)
}
barplot08()
#ref(r_case03_barplot08.png,left)
** 白黒印刷用に図を書く [#f90e5500]
barplot09 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot09.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.names.r <- paste("exp", 1:nrow(dm))
d.names.c<-paste("gene",1:ncol(dm))
d.angle <- c(20,-20) * 1:nrow(dm)
barplot(dm, beside=T, angle=d.angle, density=10, col="...
names.arg=d.names.c, legend.text=d.names.r)
}
barplot09()
#ref(r_case03_barplot09.png,left)
** 誤差範囲を表示する [#ffee2285]
標準では棒の幅1、グループ内の棒の間隔0、グループの間隔1で...
arrowsのlengthは矢印の矢じりの部分の長さを指定します。棒...
barplot10 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot10.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.names.r <- paste("exp", 1:nrow(dm))
d.names.c<-paste("gene",1:ncol(dm))
dm.error <- runif(length(dm), 0,1)
dm.error.x <- rep((nrow(dm)+1) * (1:ncol(dm) - 1), eac...
dm.error.x <- dm.error.x + rep(1:nrow(dm) + 1/2, ncol(...
barplot(dm, ylim=c(0, max(dm + dm.error)), beside=T, n...
legend.text=d.names.r)
arrows(d.error.x, dm, d.error.x, dm + dm.error, angle=...
arrows(d.error.x, dm, d.error.x, dm - dm.error, angle=...
}
barplot10()
#ref(r_case03_barplot10.png,left)
別の方法:関数barplotは各棒グラフの中点座標の配列を返すの...
barplot10 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot10.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.names.r <- paste("exp", 1:nrow(dm))
d.names.c<-paste("gene",1:ncol(dm))
dm.error <- runif(length(dm), 0,1)
# 各棒グラフの中点座標を取得
dm.error.x <- barplot(dm, ylim=c(0, max(dm + dm.erro...
legend.text=d.names.r)
arrows(dm.error.x, dm, dm.error.x, dm + dm.error, angl...
arrows(dm.error.x, dm, dm.error.x, dm - dm.error, angl...
}
barplot10()
* ヒストグラム [#o16092fb]
ヒストグラムにより、データの分布の仕方を大雑把に知るこ...
> hist(rnorm(100))
で、ヒストグラムが描かれます。 特に何も指定せずデータのみ...
** 例1 example(hist.default) 中の例 (2003.12.7) [#g5a1a0...
histmisc1 <- function() {
data(islands)
op <- par(mfrow=c(2, 2))
hist(islands)
str(hist(islands, col="gray", labels = TRUE))
hist(sqrt(islands), br = 12, col="lightblue", border...
##-- For non-equidistant breaks, counts should NOT b...
r <- hist(sqrt(islands), br = c(4*0:5, 10*3:5, 70, 1...
text(r$mids, r$density, r$counts, adj=c(.5, -.5), co...
sapply(r[2:3], sum)
sum(r$density * diff(r$breaks)) # == 1
lines(r, lty = 3, border = "purple") # -> lines.hist...
par(op)
}
#ref(グラフィックス参考実例集:棒グラフ/histmisc1.png, le...
** 例2 ヒストグラムの色々 (2003.12.10) [#n3ddf216]
既定の Sturges の方法は単に伝統があり、有名と言うだけで実...
[[Q&A]]コーナーの関連質問参照。
> x <- rnorm(1000)^2
> hist(x)
> hist(x, breaks="Sturges") # 既定の hist(x) と同一 (古...
> hist(x, breaks="scott") # Scott の方法による分割...
> hist(x, breaks="FD") # Frieman-Daiconis の方...
#ref(グラフィックス参考実例集:棒グラフ/histgram2.png, le...
** 棒グラフの枠線を消す(本体色と同じ色の枠線を描く 2004.0...
ついでに棒の間の隙間を色々変えてみる。
> par(mfrow=c(2,2))
> barplot(1:5, space=0, col="red", border="red")
> barplot(1:5, space=0.1, col="red", border="red")
> barplot(1:5, space=0.5, col="red", border="red")
> barplot(1:5, space=1, col="red", border="red")
> pp=recordPlot(); png("histnoborder.png"); replayPlot(p...
#ref(グラフィックス参考実例集:棒グラフ/histnoborder.png,...
この例では塗りつぶしと枠を同じ色にすることで解決している...
そもそも枠を描きたくないという場合はオプションで「border=...
** MASS ライブラリ中の truehist は基準化相対度数でヒスト...
truehist は総面積が1となるヒストグラム(度数を区間幅で割...
library(MASS)
data(islands)
par(mfrow=c(1,2))
hist(islands)
truehist(islands)
#ref(グラフィックス参考実例集:棒グラフ/truehist.jpg, left)
** 各棒の面積が等しくなるヒストグラム (from r-help, 2005....
quantile 関数を用い各棒の面積(標本確率)が等しくなるように...
x <- rnorm(400)
nbin <- 10 # 区間の数を10にする
hist(x,breaks=quantile(x,prob=seq(0,1,length=nbin+1)))
#ref(グラフィックス参考実例集:棒グラフ/hist.equalarea.pn...
-この方法はあまりおすすめできませんね。上の例は 400 個の...
#ref(グラフィックス参考実例集:棒グラフ/bad.png)
終了行:
COLOR(red){SIZE(20){グラフィックス参考実例集:棒グラフ、...
([[グラフィックス参考実例集]]に戻る。[[Rのグラフィックス...
#contents
~
*単純な棒グラフ [#d2840426]
barplot 関数は棒グラフを描きます。棒グラフは、棒の長さに...
引数が行列ならば各列が一本の棒となり、列の各要素の値によ...
-引数angle、density、colはそれぞれ棒を塗り分ける線分の角...
-引数legendを指定すると、右上に凡例を描きます。この引数に...
-引数namesには各棒のラベルを定める文字型ベクトル指定する...
-引数widthは各棒の相対的な幅を定めるベクトルです。spaceの...
-引数besideをTRUEにすると、引数に与えられた行列の列毎でブ...
-引数horizをTRUEにすると棒を水平にします。この場合データ...
-barplotは各棒の中心位置のx座標を返します。この値とtextな...
似たものにヒストグラムを描く hist 関数がある。見かけは似...
注意。base パッケージ中の barplot 関数 (hist も?) は対数...
** とりあえず書いてみる [#idc0114b]
barplot01 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot01.png")
d <- ceiling(runif(5, 1, 10)) # サンプルデータ
barplot(d)
}
barplot01()
#ref(r_case03_barplot01.png,left)
** x軸に名前を表示して、棒の色を変更する [#ga16adb5]
barplot03 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot03.png")
d <- ceiling(runif(5, 1, 10))
d.names <- paste("gene", 1:5) # x軸に表示される名前...
barplot(d, names.arg=d.names, col=c("blue", "green", "...
}
barplot03()
#ref(r_case03_barplot03.png,left)
** 誤差範囲を表示する [#oae18ac7]
棒グラフの棒の幅は1になっています(barplotのwidth引数)。棒...
arrowsのangleは矢印の傾きを指定します。今回は直角になるよ...
barplot04 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot04.png")
d <- ceiling(runif(5, 1, 10))
d.names <- paste("gene", 1:5)
d.error <- runif(5, 0, 1) # 誤差範囲のデータ
d.error.x <- (0.2 + 1) * 1:5 - 1/2 # エラーバーを表...
barplot(d, names.arg=d.names, col=c("blue", "green", "...
arrows(d.error.x, d, d.error.x, d + d.error, angle=90)
arrows(d.error.x, d, d.error.x, d - d.error, angle=90)
}
barplot04()
#ref(r_case03_barplot04.png,left)
別の方法:関数barplotは各棒グラフの中点座標の配列を返すの...
barplot04b <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot04b.png")
d <- ceiling(runif(5, 1, 10))
d.names <- paste("gene", 1:5)
d.error <- runif(5, 0, 1) # 誤差範囲のデータ
# 各棒グラフの中点座標を取得
d.error.x <- barplot(d, names.arg=d.names, col=c("blue...
arrows(d.error.x, d, d.error.x, d + d.error, angle=90)
arrows(d.error.x, d, d.error.x, d - d.error, angle=90)
}
barplot04b()
*積み重ねの棒グラフ [#re2e5983]
** まずは書いてみる [#y574f5e1]
barplot05 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot05.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3) # サン...
barplot(dm)
}
barplot05()
#ref(r_case03_barplot05.png,left)
** 棒グラフの間に線を引く [#i816a9db]
棒グラフの間の線はsegments関数で引きます。segmentsには線...
barplot06 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot06.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.seg.xs <- rep((0.2 + 1) * 1:(ncol(dm) - 1), each=nro...
d.seg.xe <- rep((0.2 + 1) * 2:ncol(dm) - 1, each=nrow(...
d.seg.ys <- apply(dm[,1:(ncol(dm) - 1)], 2, cumsum) ...
d.seg.ye <- apply(dm[,2:ncol(dm)], 2, cumsum) ...
barplot(dm)
segments(d.seg.xs, d.seg.ys, d.seg.xe, d.seg.ye) # ...
}
barplot06()
#ref(r_case03_barplot06.png,left)
** 凡例を表示する [#kac63249]
barplot07 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot07.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.seg.xs <- rep((0.2 + 1) * 1:(ncol(dm) - 1), each=nro...
d.seg.xe <- rep((0.2 + 1) * 2:ncol(dm) - 1, each=nrow(...
d.seg.ys <- apply(dm[,1:(ncol(dm) - 1)], 2, cumsum)
d.seg.ye <- apply(dm[,2:ncol(dm)], 2, cumsum)
d.names.r <- paste("exp", 1:nrow(dm)) # 凡例に表示...
d.names.c<-paste("gene",1:ncol(dm)) # 棒グラフのグル...
barplot(dm, names.arg=d.names.c, legend.text=d.names.r)
segments(d.seg.xs, d.seg.ys, d.seg.xe, d.seg.ye)
}
barplot07()
#ref(r_case03_barplot07.png,left)
*横に並べた棒グラフ [#m9d8e0d3]
** まずは書いてみる [#idf69853]
barplot08 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot08.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3) # サン...
barplot(dm, beside=T)
}
barplot08()
#ref(r_case03_barplot08.png,left)
** 白黒印刷用に図を書く [#f90e5500]
barplot09 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot09.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.names.r <- paste("exp", 1:nrow(dm))
d.names.c<-paste("gene",1:ncol(dm))
d.angle <- c(20,-20) * 1:nrow(dm)
barplot(dm, beside=T, angle=d.angle, density=10, col="...
names.arg=d.names.c, legend.text=d.names.r)
}
barplot09()
#ref(r_case03_barplot09.png,left)
** 誤差範囲を表示する [#ffee2285]
標準では棒の幅1、グループ内の棒の間隔0、グループの間隔1で...
arrowsのlengthは矢印の矢じりの部分の長さを指定します。棒...
barplot10 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot10.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.names.r <- paste("exp", 1:nrow(dm))
d.names.c<-paste("gene",1:ncol(dm))
dm.error <- runif(length(dm), 0,1)
dm.error.x <- rep((nrow(dm)+1) * (1:ncol(dm) - 1), eac...
dm.error.x <- dm.error.x + rep(1:nrow(dm) + 1/2, ncol(...
barplot(dm, ylim=c(0, max(dm + dm.error)), beside=T, n...
legend.text=d.names.r)
arrows(d.error.x, dm, d.error.x, dm + dm.error, angle=...
arrows(d.error.x, dm, d.error.x, dm - dm.error, angle=...
}
barplot10()
#ref(r_case03_barplot10.png,left)
別の方法:関数barplotは各棒グラフの中点座標の配列を返すの...
barplot10 <- function(){
on.exit(par(old.par <- par(no.readonly = TRUE)))
on.exit(dev.off())
png("barplot10.png")
dm <- matrix(ceiling(runif(12, 1, 10)), ncol=3)
d.names.r <- paste("exp", 1:nrow(dm))
d.names.c<-paste("gene",1:ncol(dm))
dm.error <- runif(length(dm), 0,1)
# 各棒グラフの中点座標を取得
dm.error.x <- barplot(dm, ylim=c(0, max(dm + dm.erro...
legend.text=d.names.r)
arrows(dm.error.x, dm, dm.error.x, dm + dm.error, angl...
arrows(dm.error.x, dm, dm.error.x, dm - dm.error, angl...
}
barplot10()
* ヒストグラム [#o16092fb]
ヒストグラムにより、データの分布の仕方を大雑把に知るこ...
> hist(rnorm(100))
で、ヒストグラムが描かれます。 特に何も指定せずデータのみ...
** 例1 example(hist.default) 中の例 (2003.12.7) [#g5a1a0...
histmisc1 <- function() {
data(islands)
op <- par(mfrow=c(2, 2))
hist(islands)
str(hist(islands, col="gray", labels = TRUE))
hist(sqrt(islands), br = 12, col="lightblue", border...
##-- For non-equidistant breaks, counts should NOT b...
r <- hist(sqrt(islands), br = c(4*0:5, 10*3:5, 70, 1...
text(r$mids, r$density, r$counts, adj=c(.5, -.5), co...
sapply(r[2:3], sum)
sum(r$density * diff(r$breaks)) # == 1
lines(r, lty = 3, border = "purple") # -> lines.hist...
par(op)
}
#ref(グラフィックス参考実例集:棒グラフ/histmisc1.png, le...
** 例2 ヒストグラムの色々 (2003.12.10) [#n3ddf216]
既定の Sturges の方法は単に伝統があり、有名と言うだけで実...
[[Q&A]]コーナーの関連質問参照。
> x <- rnorm(1000)^2
> hist(x)
> hist(x, breaks="Sturges") # 既定の hist(x) と同一 (古...
> hist(x, breaks="scott") # Scott の方法による分割...
> hist(x, breaks="FD") # Frieman-Daiconis の方...
#ref(グラフィックス参考実例集:棒グラフ/histgram2.png, le...
** 棒グラフの枠線を消す(本体色と同じ色の枠線を描く 2004.0...
ついでに棒の間の隙間を色々変えてみる。
> par(mfrow=c(2,2))
> barplot(1:5, space=0, col="red", border="red")
> barplot(1:5, space=0.1, col="red", border="red")
> barplot(1:5, space=0.5, col="red", border="red")
> barplot(1:5, space=1, col="red", border="red")
> pp=recordPlot(); png("histnoborder.png"); replayPlot(p...
#ref(グラフィックス参考実例集:棒グラフ/histnoborder.png,...
この例では塗りつぶしと枠を同じ色にすることで解決している...
そもそも枠を描きたくないという場合はオプションで「border=...
** MASS ライブラリ中の truehist は基準化相対度数でヒスト...
truehist は総面積が1となるヒストグラム(度数を区間幅で割...
library(MASS)
data(islands)
par(mfrow=c(1,2))
hist(islands)
truehist(islands)
#ref(グラフィックス参考実例集:棒グラフ/truehist.jpg, left)
** 各棒の面積が等しくなるヒストグラム (from r-help, 2005....
quantile 関数を用い各棒の面積(標本確率)が等しくなるように...
x <- rnorm(400)
nbin <- 10 # 区間の数を10にする
hist(x,breaks=quantile(x,prob=seq(0,1,length=nbin+1)))
#ref(グラフィックス参考実例集:棒グラフ/hist.equalarea.pn...
-この方法はあまりおすすめできませんね。上の例は 400 個の...
#ref(グラフィックス参考実例集:棒グラフ/bad.png)
ページ名: