我明白了如何在其他几个帖子的一个图表中绘制多个箱图。但我有这种情况,我无法一起绘制多个条件。我应用了与我以前的帖子(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的治疗,第二个图比较L2和L3从对照到S3,S4,S5治疗和第三个图与L4和L5比较S4和S5治疗。
这个多重条件箱图如何可能?或者我应该单独制作这些箱图,然后将它们放在同一个图表中?
答案 0 :(得分:3)
我不确定这是否是你想要的,但它需要一些数据操作。
如果您想在(L1,S1,S2 | L2,L3,S3,S4,S5 | L4,L5,S4,S5)的第四列(即“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')