【发布时间】:2019-03-30 12:19:39
【问题描述】:
我希望在 x 轴上添加第二层分组,如下面的结果 A 面板所示。每种估计类型(ITT 与 TOT)应该有两个点对应于标签 3 或 12。
这是我获得所见内容的方法,减去对结果 A 面板的编辑:
df %>%
ggplot(., aes(x=factor(estimate), y=gd, group=interaction(estimate, time), shape=estimate)) +
geom_point(position=position_dodge(width=0.5)) +
geom_errorbar(aes(ymin=gd.lwr, ymax=gd.upr), width=0.1,
position=position_dodge(width=0.5)) +
geom_hline(yintercept=0) +
ylim(-1, 1) +
facet_wrap(~outcome, scales='free', strip.position = "top") +
theme_bw() +
theme(panel.grid = element_blank()) +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside")
这是玩具数据:
df <- structure(list(outcome = c("Outcome C", "Outcome C", "Outcome C",
"Outcome C", "Outcome B", "Outcome B", "Outcome B", "Outcome B",
"Outcome A", "Outcome A", "Outcome A", "Outcome A"), estimate = c("ITT",
"ITT", "TOT", "TOT", "ITT", "ITT", "TOT", "TOT", "ITT", "ITT",
"TOT", "TOT"), time = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("3", "12"), class = "factor"),
gd = c(0.12, -0.05, 0.19, -0.08, -0.22, -0.05, -0.34, -0.07,
0.02, -0.02, 0.03, -0.03), gd.lwr = c(-0.07, -0.28, -0.11,
-0.45, -0.43, -0.27, -0.69, -0.42, -0.21, -0.22, -0.33, -0.36
), gd.upr = c(0.31, 0.18, 0.5, 0.29, 0, 0.17, 0.01, 0.27,
0.24, 0.19, 0.38, 0.3)), class = "data.frame", row.names = c(NA,
-12L))
【问题讨论】:
-
谢谢,@iod。我遇到了这个答案,但我认为考虑到自 2010 年以来 ggplot 的所有发展,可能会有更好的方法。
-
谢谢,@Mike。在您指出的示例中,只有 1 个刻面术语,但我认为我至少需要使用 2 个,因为我已经在
outcome上刻面。结果看起来不太正确。 -
好点,@Roman。我简化了数据框。