species <- c("A","B","A","C","D","E","A","B","E","A","C","B","D")
length <- c(13,20,14,18,18,13,14,17,18,14,19,20,15)
color <- c(1,1,1,2,2,1,1,2,1,2,2,2,1)
sex <- c(1,1,1,1,1,1,1,2,2,2,2,2,2)
data <- data.frame(species = species, length = length, color = color, sex = sex)
data <- data[data$species =="A"|data$species =="B"|data$species =="D",]
data $species <- factor(data$species)
par(mfrow=c(2,2), mar=c(2.2,2,1,1), oma=c(1,3,1,1))
for(i in 1:2) {
for (j in 1:2) {
selected <- is.element(data$color,i) & is.element(data$sex,j)
print(sum(selected))
boxplot(length[selected]~(species[selected]),data=data,ylim=c(10,30),mtext("length", side=2, line=1,outer=TRUE))
}}
No.01995 Re: R:boxplotにmean値を追加 【青木繁伸】 2006/12/22(Fri) 18:28
points 関数を使った行を追加
それと,mtext の位置に注意(そもそも,ループの外にあるべきですよ)set.seed(12345)
species <- sample(LETTERS[1:5], 100, replace=TRUE)
length <- rnorm(100, mean=16.38, sd=2.63)
color <- sample(2, 100, replace=TRUE)
sex <- sample(2, 100, replace=TRUE)
data <- data.frame(species = species, length = length, color = color, sex = sex)
data <- data[data$species =="A"|data$species =="B"|data$species =="D",]
data$species <- factor(data$species)
print(data)
par(mfrow=c(2,2), mar=c(2.2,2,1,1), oma=c(1,3,1,1))
for(i in 1:2) {
for (j in 1:2) {
selected <- is.element(data$color,i) & is.element(data$sex,j)
boxplot(length[selected]~species[selected],data=data,ylim=c(10,30))
points(by(data$length[selected], data$species[selected], mean), col="red")
}
}
mtext("length", side=2, line=1,outer=TRUE)
No.01996 Re: R:boxplotにmean値を追加 【takahashi】 2006/12/22(Fri) 18:46
あまり参考にならないかと思いますがRっぽくone-linerでspecies <- c("A","B","A","C","D","E","A","B","E","A","C","B","D")
length <- c(13,20,14,18,18,13,14,17,18,14,19,20,15)
color <- c(1,1,1,2,2,1,1,2,1,2,2,2,1)
sex <- c(1,1,1,1,1,1,1,2,2,2,2,2,2)
data <- data.frame(species, length, color, sex)
data <- data[data$species %in% c("A","B","D"),]
data$species <- factor(data$species)
par(mfrow=c(2,2), mar=c(2.2,2,1,1), oma=c(1,3,1,1))
by(data,list(data$color,data$sex),function(x){boxplot(length~species,x,ylim=c(10,30));points(by(x$length,x$species,mean),col="red")})
mtext("length", side=2, line=1,outer=TRUE)
No.01997 Re: R:boxplotにmean値を追加 【青木繁伸】 2006/12/22(Fri) 18:57
秘密にしてありますが,<pre>〜</pre>が使えます (^_^)
● 「統計学関連なんでもあり」の過去ログ--- 039 の目次へジャンプ
● 「統計学関連なんでもあり」の目次へジャンプ
● 直前のページへ戻る