无法分面包装

时间:2018-10-04 08:54:04

标签: r ggplot2

我正在尝试绘制与x轴值的平均值相交的geom_vline。我正在尝试在每个构面窗格上绘制两行:一行表示Experimental的平均值,另一行表示Historical Control的平均值。

因此,均值应同时由构面(PROC)和颜色aesHC)来计算。

ggplot(data = df) +
  geom_jitter(mapping = aes(x = TOTAL_ME/LOS, y = LOS, color = HC), alpha = 0.75, height = 0.2) +
  labs(color = "Treatment group",  
       x = "Morphine equivalents per day", 
       y = "Length of stay (days)") +
  scale_color_discrete(breaks=c("EXPERIMENTAL", "HISTORICAL CONTROL"),
                       labels=c("Experimental", "Historical Control")) +
  coord_cartesian(ylim=c(0, 4), xlim = c(0,50)) +
  geom_vline(xintercept = mean(df$TOTAL_ME/df$LOS)) +
  facet_wrap(~ PROC)

我只能弄清楚如何在所有三个多面窗格上绘制总体总体平均值。我无法绘制多面人口或HC的平均值。enter image description here

链接的可能重复问题仍然无法回答问题。通过遵循这些问题的建议:

hcmeans <- df %>%
  group_by(HC) %>%
  summarise(MN = mean(TOTAL_ME/LOS))

ggplot(data = df) +
  geom_jitter(mapping = aes(x = TOTAL_ME/LOS, y = LOS, color = HC), alpha = 0.75, height = 0.2) +
  labs(color = "Treatment group",  
       x = "Morphine equivalents per day", 
       y = "Length of stay (days)") +
  scale_color_discrete(breaks=c("EXPERIMENTAL", "HISTORICAL CONTROL"),
                       labels=c("Experimental", "Historical Control")) +
  coord_cartesian(ylim=c(0, 4), xlim = c(0,50)) +
  geom_vline(data = hcmeans, aes(xintercept = MN)) +
  facet_wrap(~ PROC)

enter image description here

现在我在每个构面窗格中得到两个geom_vline,但是它们的含义相同。它们是所有Experimental和所有Historical Control的均值,而不仅仅是Experimental ControlLAP RYNGB组的均值,等等...

没关系,我回答了我自己的问题:

hcmeans <- df %>%
  group_by(HC, PROC) %>%
  summarise(MN = mean(TOTAL_ME/LOS))

只需将PROC添加到group_by。

感谢指导。

enter image description here

0 个答案:

没有答案