ggplot2 / R中每个x值组合两个箱形图的困难

时间:2019-05-14 10:17:01

标签: r ggplot2

请在下面找到My Data

我希望生成一个组合箱形图,如下所示:

enter image description here

我在w$WHO==1,2,3,4中有四种不同的患者类别,每种类别分别对应于WHO-I,WHO-II,WHO-III和未知肿瘤。

我希望每个w$WHO产生两个箱形图

第一个箱线图应显示w$Follow.up.death[w$Death==1]中的死亡时间。我想将颜色/轮廓+填充颜色设置为蓝色,但是填充颜色的alpha值应为0.2.2。

第二个箱线图应显示进展时间,如w$resp.time[w$response==1]中所示。我想将颜色/轮廓线+填充颜色设为红色,但填充颜色应为alpha = 0.2。

w$Deathw$response的长度不同。

相应的图例应显示“总体生存期”和“无进展生存期”。

我尝试了几本手册,但是我不知道该怎么做-我只设法制作了一张图。

有什么办法解决这个问题吗?

My data

的示例
w <- structure(list(WHO = c(1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 
3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 4L, 4L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 4L, 4L, 4L, 
4L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 2L, 1L, 2L, 2L, 4L, 4L, 
4L, 2L, 4L, 1L, 4L, 4L, 2L, 4L, 4L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L
), Death = c(0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 
0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), Follow.up.death = c(18, 
2, 14, 17, 31, 4, 20, 15, 12, 19, 10, 17, 27, 22, 3, 43, 24, 
14, 13, 5, 12, 137, 22, 87, 48, 24, 72, 32, 14, 83, 68, 56, 57, 
18, 16, 70, 1.9, 69.2, 126.3, 41.6, 17.9, 1.3, 87.4, 4.4, 137.4, 
17.5, 95.8, 65.2, 14.8, 98.5, 16.6, 74.9, 10.3, 43.4, 32.5, 4.8, 
7.3, 107.8, 6.8, 18.3, 33, 25.2, 49.2, 15.9, 1.2, 42.7, 1, 9, 
1.8, 15.6, 8.9, 15, 16.4, 7.7, 75.5, 12.2, 54.8, 22.2, 9.7, 14.3, 
5.2, 64.5, 21.8, 0.2, 7.3, 18.7, 5.1, 17.3, 27.4, 16, 24.2, 9.7, 
8.2, 5.7, 41.8, 10.6, 22.8, 4.8, 6, 4, 50, 21, 30, 5, 11, 12), 
    response = c(0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 
    1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, 
    0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 
    0L, 0L, 0L, 0L, 0L), resp.time = c(18, 2, 13, 17, 22, 2, 
    6, 5, 12, 8, 3, 2, 1, 21, 2, 43, 4, 2, 4, 5, 0.1, 137, 4, 
    87, 17, 24, 72, 19, 14, 83, 68, 56, 57, 18, 14, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, 15.6, 8.9, 15, 16.4, 7.7, 75.5, 3, 54.8, 22.2, 12, 14.3, 
    6, 12, 21.8, 6, 3, 12, 3, 6, 3, 3, 12, 9.7, 3, 3, 12, 3, 
    6, 3, 6, 4, 50, 21, 30, 5, 11, 12)), .Names = c("WHO", "Death", 
"Follow.up.death", "response", "resp.time"), class = "data.frame", row.names = c(NA, 
-106L))

2 个答案:

答案 0 :(得分:3)

这接近吗?

# Wrangling data into a digestable format
v <- w[w$Death == 1,]
vv <- w[w$response == 1,]
df <- data.frame(x = as.factor(c(v$WHO, vv$WHO)),
                 y = c(v$Follow.up.death, vv$resp.time),
                 f = rep(c("Death", "Response"), c(nrow(v), nrow(vv))))
df <- df[!is.na(df$x),]

然后进行绘制:

ggplot(df) +
  geom_boxplot(aes(x, y, fill = f, colour = f)) +
  scale_x_discrete(name = "WHO") +
  scale_y_continuous(name = "Time") +
  # Alpha encoded as hex values as to force to apply on fill only
  # Not sure if alpha = 0.2 corresponds to '33'
  scale_fill_manual(values = c("#FF000033", "#0000FF33"), name = "Survival",
                    labels = c("Overall", "Progression-free")) +
  scale_colour_manual(values = c("red", "blue"), name = "Survival",
                      labels = c("Overall", "Progression-free"))

enter image description here

答案 1 :(得分:2)

类似于@tuenbrand的方法,但是使用dplyr/tidyr来纠缠数据:

library(tidyverse)
df <- w %>% 
  rename(Overall = "Follow.up.death", `Progression-free` = "resp.time") %>% 
  gather(key = Survival, value = Time, Overall, `Progression-free`) %>%
  filter((Death == 1 & Survival == "Overall") | 
         (response == 1 & Survival == "Progression-free")) %>% 
  mutate(WHO = paste("WHO:", WHO))

然后在WHO上进行构面以分隔箱形图:

ggplot(df, aes(x = Survival, y = Time, fill = Survival)) + 
  geom_boxplot() + facet_wrap(~WHO, nrow = 1) +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.x = element_blank(),
        legend.position = "bottom")

enter image description here