我们可以在base R中使用boxplot(),这里我们需要先使用reshape()来获得长格式。
boxplot(gene ~ Status + time + Treatment,
reshape(cbind(id=rownames(dat), dat), 4:5, sep="", direction="long"),
border=1:2)
但是,这会产生一个非常拥挤的情节。我们可以做单独的箱线图,例如每个治疗组使用sapply()。
par(mfrow=c(2, 2))
sapply(unique(dat$Treatment), function(x) {
boxplot(value ~ Status + gene,
reshape(cbind(id=rownames(dat[dat$Treatment == x, ]), dat[dat$Treatment == x, ]),
4:5, sep="", direction="long", v.names="value", timevar="gene"),
at=c(1:2, 4:5),
main=x,
border=1:2)
})
结果
数据
dat <- structure(list(Treatment = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L,
4L, 4L), .Label = c("Both", "Chemo", "None", "Radio"), class = "factor"),
Status = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Censored", "Deceased"), class = "factor"),
gene1 = c(2.83185327992901, 5.21658677992433, 9.36719279899948,
1.77809421116808, 6.39453760571561, 3.08376117126782, -1.99524072673447,
0.380722587753265, -0.947148460332481, 1.73014054712629,
0.855919162512028, 0.501667581598007, 0.0638735169737497,
10.1712355237258, 5.34317645471502, -7.96626158445742, -0.0781613844302278,
5.59930916967042, -0.725717330717595, 0.492793009977729,
-0.546677404630108, 0.290301979542245, 2.83540215865274,
-1.25738031049913), gene2 = c(6.97361394841868, -6.86012827859373,
-0.193731972798249, -5.64669185350061, -20.6664537342379,
32.5477488386544, 12.6210452154023, 6.56845245925654, 13.5491140544121,
-2.9113829554538, 2.90958200298303, -6.56806056188421, 50.2577234864485,
17.0734922804668, 49.0769939658538, -2.0186433516603, 32.3823429023035,
17.7654319738005, 12.2884241568455, 21.7600566866782, 19.68978862329,
-12.6277420840716, 27.555120882401, 17.5164450232983)), row.names = c(3L,
23L, 13L, 44L, 34L, 50L, 90L, 67L, 62L, 100L, 95L, 96L, 132L,
144L, 124L, 174L, 171L, 168L, 196L, 205L, 207L, 233L, 229L, 212L
), class = "data.frame")