如何通过仅显示数据框中的某些行来创建多个箱线图
How to create multipe boxplots in one by only chosing certain rows from a data frame
我想做的是创建几个箱线图(全部显示在一个箱线图中)仅根据我原始数据框的某些值。
我的数据框如下所示:
enter image description here
所以现在我想让 R 可视化 Parameter ~ Station(参数都是绿色变量,Station 是 "station id")
有没有办法告诉 R 我想要 x 轴上的所有参数 ONLY for BB0028 例如,这意味着我只将 mean_area、mean_area_exc、esd、feret、min 和 max 的前 6 个值考虑在内箱形图?
那看起来像这样:
enter image description here
我尝试过以非常复杂的方式逐个添加单个箱线图,但我相信一定有更简单的方法。
这是我试过的:
bb28 <- df[c(1:6),]
bb28area <- boxplot(bb28$mean_area ~ bb28$BBnr)
bb28area_exc <- boxplot(bb28$mean_area_exc ~ bb28$BBnr)
bb28esd <- boxplot(bb28$mean_esd ~ bb28$BBnr)
bb28feret <- boxplot(bb28$mean_feret ~ bb28$BBnr)
bb28min <- boxplot(bb28$mean_min ~ bb28$BBnr)
bb28max <- boxplot(bb28$mean_max ~ bb28$BBnr)
boxplot(bb28$mean_area ~ bb28$BBnr)
boxplot(bb28$mean_area_exc ~ bb28$BBnr, add=TRUE, at = 1:1+0.45)
它看起来也不太好,因为在图中 x 轴没有调整到新的箱线图,然后被切断:
enter image description here
我希望你能帮助我用简单的正确代码来获得我的情节。
谢谢!
干杯,默尔
也许下面的函数 multi.boxplot
就是您要找的。它仅使用基数 R。
数据.
首先,制作一个数据集,因为您没有以易于复制和粘贴的格式向我们提供数据集。
set.seed(1234)
n <- 50
BBnr <- sort(sprintf("BB%04d", sample(28:30, n, TRUE)))
bb28 <- data.frame(col1 = 1:n, col2 = n:1, BBnr = BBnr)
tmp <- matrix(runif(3*n), ncol = 3)
colnames(tmp) <- paste("mean", c("this", "that", "other"), sep = "_")
bb28 <- cbind(bb28, tmp)
rm(BBnr, tmp)
代码.
multi.boxplot <- function(x, by, col=0, ...){
x <- as.data.frame(x)
uniq.by <- unique(by)
len <- length(uniq.by) - 1
n <- ncol(x)
n1 <- n + 1
col <- rep(col, n)[seq_len(n)]
boxplot(x[[ 1 ]] ~ by, at = 0:len*n1 + 1,
xlim = c(0, (len + 1)*n1), ylim = range(unlist(x)), xaxt = "n", col=col[1], ...)
for(i in seq_len(n)[-1])
boxplot(x[[i]] ~ by, at = 0:len*n1 + i, xaxt = "n", add = TRUE, col=col[i], ...)
axis(1, at = 0:len*n1 + n1/2, labels = uniq.by, tick = TRUE)
}
inx <- grep("mean", names(bb28))
multi.boxplot(bb28[inx], by = bb28$BBnr, col = rainbow(length(inx)))
我想做的是创建几个箱线图(全部显示在一个箱线图中)仅根据我原始数据框的某些值。
我的数据框如下所示: enter image description here
所以现在我想让 R 可视化 Parameter ~ Station(参数都是绿色变量,Station 是 "station id") 有没有办法告诉 R 我想要 x 轴上的所有参数 ONLY for BB0028 例如,这意味着我只将 mean_area、mean_area_exc、esd、feret、min 和 max 的前 6 个值考虑在内箱形图? 那看起来像这样: enter image description here
我尝试过以非常复杂的方式逐个添加单个箱线图,但我相信一定有更简单的方法。 这是我试过的:
bb28 <- df[c(1:6),]
bb28area <- boxplot(bb28$mean_area ~ bb28$BBnr)
bb28area_exc <- boxplot(bb28$mean_area_exc ~ bb28$BBnr)
bb28esd <- boxplot(bb28$mean_esd ~ bb28$BBnr)
bb28feret <- boxplot(bb28$mean_feret ~ bb28$BBnr)
bb28min <- boxplot(bb28$mean_min ~ bb28$BBnr)
bb28max <- boxplot(bb28$mean_max ~ bb28$BBnr)
boxplot(bb28$mean_area ~ bb28$BBnr)
boxplot(bb28$mean_area_exc ~ bb28$BBnr, add=TRUE, at = 1:1+0.45)
它看起来也不太好,因为在图中 x 轴没有调整到新的箱线图,然后被切断: enter image description here
我希望你能帮助我用简单的正确代码来获得我的情节。
谢谢! 干杯,默尔
也许下面的函数 multi.boxplot
就是您要找的。它仅使用基数 R。
数据. 首先,制作一个数据集,因为您没有以易于复制和粘贴的格式向我们提供数据集。
set.seed(1234)
n <- 50
BBnr <- sort(sprintf("BB%04d", sample(28:30, n, TRUE)))
bb28 <- data.frame(col1 = 1:n, col2 = n:1, BBnr = BBnr)
tmp <- matrix(runif(3*n), ncol = 3)
colnames(tmp) <- paste("mean", c("this", "that", "other"), sep = "_")
bb28 <- cbind(bb28, tmp)
rm(BBnr, tmp)
代码.
multi.boxplot <- function(x, by, col=0, ...){
x <- as.data.frame(x)
uniq.by <- unique(by)
len <- length(uniq.by) - 1
n <- ncol(x)
n1 <- n + 1
col <- rep(col, n)[seq_len(n)]
boxplot(x[[ 1 ]] ~ by, at = 0:len*n1 + 1,
xlim = c(0, (len + 1)*n1), ylim = range(unlist(x)), xaxt = "n", col=col[1], ...)
for(i in seq_len(n)[-1])
boxplot(x[[i]] ~ by, at = 0:len*n1 + i, xaxt = "n", add = TRUE, col=col[i], ...)
axis(1, at = 0:len*n1 + n1/2, labels = uniq.by, tick = TRUE)
}
inx <- grep("mean", names(bb28))
multi.boxplot(bb28[inx], by = bb28$BBnr, col = rainbow(length(inx)))