将多面图与常见多面相结合

时间:2018-10-04 11:03:31

标签: r ggplot2

p4 <- ggplot(data = df.2) +
  geom_jitter(mapping = aes(x = TOTAL_ME/LOS, y = LOS, color = HC), alpha = 0.3, height = 0.2) +
  labs(color = "Treatment group",  
       x = "Morphine equivalents per day", 
       y = "Length of stay (days)",
       tag = "B") +
  scale_color_discrete(breaks=c("EXPERIMENTAL", "HISTORICAL CONTROL"),
                       labels=c("Experimental", "Historical Control")) +
  coord_cartesian(ylim=c(0, 4), xlim = c(0,50)) +
  geom_tile(data = means, mapping = aes(x = meMN, y = losMN, width = wid*2, height = hght*2,
                                        fill = HC), alpha = 0.2) +
  geom_point(data = means, mapping = aes(x = meMN, y = losMN, color = HC), shape = 3) + 
  guides(fill = FALSE) +
  facet_wrap(~ PROC.FULL, labeller = labeller(PROC.FULL = label_wrap_gen(15)))

ggarrange(p3, p4, ncol=1, nrow=2, common.legend = TRUE, legend = "bottom")

p3与上图相同,只有ggplot(data = df)

p3p4组合在一起时,我得到以下图:

enter image description here

理想情况下,我只希望沿着底部的一个x轴,而只希望顶部的一个刻面标记。我试图仅从p4中删除构面标签,但这实际上使底行与顶行的大小不相等。我想在图的右侧添加一个标签,将顶部行标记为p3,将底部行标记为p4。最好在左侧仅包含一个Y轴标签,该标签跨越两行。本质上,我希望将这两个单独的图(由两个不同的data.frame生成)组合起来,并删除尽可能多的冗余信息。

更新:我已经按照建议的方式绑定了数据帧,而且看起来似乎更近了。我仍然有多余标签的问题。另外,我希望这些小面垂直对齐(“腹腔镜Roux-En-Y胃旁路”应该一个接一个),并在右侧逐行标记id_col

df.3 <- bind_rows(df, df.2, .id = "id_col")

ggplot(data = df.3) +
  geom_jitter(mapping = aes(x = TOTAL_ME/LOS, y = LOS, color = HC), alpha = 0.3, height = 0.2) +
  labs(color = "Treatment group",  
       x = "Morphine equivalents per day", 
       y = "Length of stay (days)",
       tag = "B") +
  scale_color_discrete(breaks=c("EXPERIMENTAL", "HISTORICAL CONTROL"),
                       labels=c("Experimental", "Historical Control")) +
  coord_cartesian(ylim=c(0, 4), xlim = c(0,50)) +
  geom_tile(data = means, mapping = aes(x = meMN, y = losMN, width = wid*2, height = hght*2,
                                        fill = HC), alpha = 0.2) +
  geom_point(data = means, mapping = aes(x = meMN, y = losMN, color = HC), shape = 3) + 
  guides(fill = FALSE) +
  facet_wrap(~ id_col + PROC.FULL, labeller = labeller(PROC.FULL = label_wrap_gen(15)))

enter image description here

UPDATE2:

解决了!只需使用facet_grid,而不是facet_wrap

0 个答案:

没有答案