我想在 R 中显示带有平均值值的箱形图。我的箱形图代表6个不同地点的过氧化氢酶活性,我想从北到南重新排序,最后有一个参考站点(N1,N2,M1,M2,S1,S2,R)。我可以轻松地重新排序这样的箱形图:
levels(data$site)
[1] "S2" "N1" "R" "N2" "S1" "M1" "M2"
x = factor(data$site,levels(data$site)[c(2,4,6,7,5,1,3)])
boxplot(catalase~x, data)
但意味着值并不遵循新的排序。 我这样称为平均值:
means<-tapply(data$catalase, data$site, mean)
points(means, col="red", pch=19)
并试着像这样重新排序:
reordered_means = factor(means,levels(means)[c(2,4,6,7,5,1,3)])
但很明显,这并没有成功,因为它不起作用。 建议,拜托?
答案 0 :(得分:0)
你需要的不是
reordered_means = factor(means,levels(means)[c(2,4,6,7,5,1,3)])
但
reordered_means = means[c(2,4,6,7,5,1,3)]
示例强>
## toy data: z ~ site
site <- gl(7 , 5, labels = c("S2", "N1", "R", "N2", "S1", "M1", "M2"))
set.seed(0); z <- round(rnorm(35), 2)
## re-level
piv <- c(2,4,6,7,5,1,3) ## pivoting index
x <- factor(site, levels(site)[piv])
## old mean before re-levelling
means <- tapply(z, site, mean)
## new mean after re-levelling
reordered_means <- means[piv]
# N1 N2 M1 M2 S1 S2 R
#-0.074 -0.370 -0.066 -0.226 0.206 0.788 -0.356
## we can verify this by computing from scratch
reordered_means <- tapply(z, x, mean)
# N1 N2 M1 M2 S1 S2 R
#-0.074 -0.370 -0.066 -0.226 0.206 0.788 -0.356
答案 1 :(得分:0)
您只需在重新排序数据集后重新计算均值(也可能纠正代码中的一些小错误)
data(mtcars)
mtcars$gear <- factor(mtcars$gear)
boxplot(mpg~gear, mtcars)
means=tapply(mtcars$mpg, mtcars$gear, mean)
points(means, col="red", pch=19)
mtcars$gear <- factor(mtcars$gear, levels=levels(mtcars$gear)[c(2,3,1)])
means=tapply(mtcars$mpg, mtcars$gear, mean)
boxplot(mpg~gear, mtcars)
points(means, col="red", pch=19)