gpt4 book ai didi

r - 向 x 轴添加嵌套/分组级别

转载 作者:行者123 更新时间:2023-12-02 06:48:00 24 4
gpt4 key购买 nike

我希望在 x 轴上添加第二层分组,如下面的结果 A 面板所示。每种估计类型(ITT 与 TOT)应该有两个点对应于标签 3 或 12。

enter image description here

这是我获得您所见内容的方法,减去对结果 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))

最佳答案

1

x 美学更改为 interaction(time, factor(estimate)) 并添加了合适的离散标签。

df %>%
ggplot(., aes(x = interaction(time, factor(estimate)), # relevant
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") +
scale_x_discrete(labels = c("3\nITT", "12\nITT", "3\nTOT", "12\nTOT")) # relevant

关于r - 向 x 轴添加嵌套/分组级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52996857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com