我理解了如何从其他几个帖子中在一个图表中绘制多个盒子图。但是我有这样的情况,我无法在一起画出多个条件。我应用了与我以前的帖子(Multiple boxplots in R)相同的想法,但在这种情况下不起作用。
我有这个数据集
Control Treatment
L1 L2 L3 L4 L5 S1 S2 S3 S4 S5
g1 10.5 12 10 11 12 13 14 10 11 12
g2 11 13 10 10 11 10.5 12 8 9 10
g3 10 9 9 8 9 11 10 11 9 11
g4 9 8 8 9 8 6 5 5 7 6
g5 16 4 6.5 6.8 5 4 6 6 8 9
g6 11 12 7.8 7.5 6 5 4 9 10 11
g7 10 6 8.9 6.4 7.2 13 12 12 12 10
g8 5 4 9.0 5.6 7.8 12 12 9 8 7
g9 11 12 11 8.5 7.4 10 11.5 8 7 6
g10 8.9 7.8 13 5.6 6.8 7.6 5.8 5 4 5 并希望将多个条件表示为同一图中的多个方格图。
在治疗中,我想把L1从对照到S1和S2的第一个比较区和从对照到S3、S4、S5的L2和L3的第二个小区,以及与S4和S5相比的L4和L5的第三个小区。
怎么可能出现这个多条件箱形图?或者我应该把这些方块分开,然后把它们放在同一张图上呢?
发布于 2013-10-31 12:05:11
我不确定这是否是您要寻找的,但它需要一点数据操作。
如果要在第四列(即"Group2")中手动输入分组,则需要复制S4 & S5行,并将它们放在适当的组中。然后你会改变:
facet_wrap( ~ Group2, scales = 'free')--
library(ggplot2)
library(reshape2)
control <- ## read in control data
control$group <- rep('control', nrow(control))
control <- melt(control, id.vars = 'group')
treatment <- ## read in control data
treatment$group <- rep('treatment', nrow(treatment))
treatment <- melt(treatment, id.vars = 'group')
allData <- rbind(control, treatment)
ggplot(allData, aes(x = variable, y = value, group = variable)) +
geom_boxplot() +
facet_wrap( ~ group, scales = 'free')

-更新--
library(gdata)
library(reshape2)
library(ggplot2)
control <- ## read in control data
control$group <- rep('control', nrow(control))
control <- melt(control, id.vars = 'group')
treatment <- ## read in treatment data
treatment$group <- rep('treatment', nrow(treatment))
treatment <- melt(treatment, id.vars = 'group')
allData <- rbind(control, treatment)
compA <- subset(allData,
variable == 'L1' |
variable == 'S1' |
variable == 'S2')
compB <- subset(allData,
variable == 'L2' |
variable == 'L3' |
variable == 'S3' |
variable == 'S4' |
variable == 'S5')
compC <- subset(allData,
variable == 'L4' |
variable == 'L5' |
variable == 'S4' |
variable == 'S5')
allData <- combine(compA, compB, compC)
ggplot(allData, aes(x = variable, y = value, group = variable, fill = group)) +
geom_boxplot() +
facet_wrap( ~ source, scales = 'free_x')

https://stackoverflow.com/questions/19705039
复制相似问题