分组箱线图 [R]
box plot in groups [R]
我有表格的数据
x <- matrix(rnorm(600), nrow = 100, ncol = 6)
x <- cbind(x, c(rep(1, 50), rep(2, 50)))
colnames(x) <- c("a", "b", "c", "d", "e", "f", "group")
每列的箱线图(没有 "outlying" 点)可以这样制作:
library(ggplot2)
x <- as.data.frame(x)
xmelt <- melt(x)
boxplot(data = xmelt, value~variable, outlwd = 0)
我想要一个由 6 组箱线图组成的图,按 "a"、"b"、...、"f" 分组,其中每组(例如 "a") 的箱线图的值 "a" 对应于 "group" 的不同值。这应该可以使用 ggplot2,但我不断收到错误。作为画龙点睛之笔,需要使用 "group" 变量对箱线图进行着色。因此,在每个字母 "a"、...、"f" 上方,有一组 2 个(如果 "group" 具有更多不同的值,则更多)的箱线图根据"group" 值。
使用基数 boxplot
可能可行,但使用 ggplot2
更容易。
你可以像以前一样使用reshape2::melt
,但是指定组为id.vars
,然后给组加个美感
ggplot(melt(x, id.vars='group')) +
geom_boxplot(aes(variable, value, color=factor(group)), outlier.colour=NA)
已编辑添加 要像在您的 boxplot
调用中一样删除异常值,请使用 outlier.colour
(根据 this answer)。 outlier.color
也有效,至少在 ggplot2 2.1.0
.
我有表格的数据
x <- matrix(rnorm(600), nrow = 100, ncol = 6)
x <- cbind(x, c(rep(1, 50), rep(2, 50)))
colnames(x) <- c("a", "b", "c", "d", "e", "f", "group")
每列的箱线图(没有 "outlying" 点)可以这样制作:
library(ggplot2)
x <- as.data.frame(x)
xmelt <- melt(x)
boxplot(data = xmelt, value~variable, outlwd = 0)
我想要一个由 6 组箱线图组成的图,按 "a"、"b"、...、"f" 分组,其中每组(例如 "a") 的箱线图的值 "a" 对应于 "group" 的不同值。这应该可以使用 ggplot2,但我不断收到错误。作为画龙点睛之笔,需要使用 "group" 变量对箱线图进行着色。因此,在每个字母 "a"、...、"f" 上方,有一组 2 个(如果 "group" 具有更多不同的值,则更多)的箱线图根据"group" 值。
使用基数 boxplot
可能可行,但使用 ggplot2
更容易。
你可以像以前一样使用reshape2::melt
,但是指定组为id.vars
,然后给组加个美感
ggplot(melt(x, id.vars='group')) +
geom_boxplot(aes(variable, value, color=factor(group)), outlier.colour=NA)
已编辑添加 要像在您的 boxplot
调用中一样删除异常值,请使用 outlier.colour
(根据 this answer)。 outlier.color
也有效,至少在 ggplot2 2.1.0
.