我正在尝试绘制与x轴值的平均值相交的geom_vline
。我正在尝试在每个构面窗格上绘制两行:一行表示Experimental
的平均值,另一行表示Historical Control
的平均值。
因此,均值应同时由构面(PROC
)和颜色aes
(HC
)来计算。
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
的平均值。
链接的可能重复问题仍然无法回答问题。通过遵循这些问题的建议:
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)
现在我在每个构面窗格中得到两个geom_vline
,但是它们的含义相同。它们是所有Experimental
和所有Historical Control
的均值,而不仅仅是Experimental Control
和LAP RYNGB
组的均值,等等...
没关系,我回答了我自己的问题:
hcmeans <- df %>%
group_by(HC, PROC) %>%
summarise(MN = mean(TOTAL_ME/LOS))
只需将PROC
添加到group_by。
感谢指导。