I have data of the form
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")
A boxplot (without the "outlying" dots) of each column can be made like this:
library(ggplot2)
x <- as.data.frame(x)
xmelt <- melt(x)
boxplot(data = xmelt, value~variable, outlwd = 0)
I would like to have a plot consisting of 6 groups of boxplots, grouped by "a", "b", ..., "f", where each group (e.g. "a") has the boxplots with the values of "a" for the different values of "group". This should be possible using ggplot2, but I keep getting errors. And as finishing touch the boxplots need to be coloured using the "group"-variable. Thus, above each letter "a", ..., "f" there is a group of 2 (or more if "group" takes on more different values") boxplots that take a color according to the "group" value.
答案 0 :(得分:3)
这可能是使用基础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
中。