【发布时间】:2015-02-13 08:17:00
【问题描述】:
我想知道是否可以使用 apply 函数生成一组类似于嵌套循环组合生成的箱线图。
这可能是不可能/必要的,但我认为它应该是可能的,我只是不知道该怎么做。
我需要能够对此进行绘制,以查看 100 个因素如何比较一个变量 (mtcars$mpg)
head(mtcars)
for (i in 8:11) {
for (j in 8:11) {
if (i != j) {
title = paste(names(mtcars)[i], names(mtcars)[j],
sep = "/")
p <- ggplot(mtcars, aes(interaction(mtcars[,i], mtcars[, j]), mpg, fill = factor(mtcars[,i]))) + geom_boxplot(alpha = I(0.7))
p <- p + ggtitle(title) + scale_fill_hue()
} else {
title = paste(names(mtcars)[i])
p <- ggplot(mtcars, aes(factor(mtcars[,i]), mpg, fill = factor(mtcars[, i]))) + geom_boxplot(alpha = I(0.7))
p <- p + ggtitle(title) + scale_fill_hue()
}
print(p)
}
}
【问题讨论】: