我正在创建一个图表,显示每个变量的 4 个结果 beta + CI 线。
我想更清楚地表明 4 个标绘点(结果)和线(CI)与每个变量相关。
因此,我很好奇如何 1) 做出更清晰的面板或网格更改以显示哪 4 个结果与哪个变量相关,或者 2) 在 y 轴上围绕变量创建括号?
代码和图如下。
library(tidyverse)
library(viridis)
#> Loading required package: viridisLite
df <- tribble(
~var, ~gender, ~outcome, ~est, ~l95, ~u95,
"A", "Men", "X", 0.2, 0.1, 0.3,
"A", "Men", "Y", 0.3, 0.25, 0.35,
"B", "Men", "X", -0.4, -0.5, -0.3,
"B", "Men", "Y", -0.45, -0.5, -0.4,
"A", "Women", "X", 0.4, 0.3, 0.5,
"A", "Women", "Y", 0.6, 0.55, 0.65,
"B", "Women", "X", -0.1, -0.2, 0,
"B", "Women", "Y", -0.3, -0.4, -0.2,
"A", "Men", "Z", 0.2, 0.1, 0.3,
"A", "Men", "AA", 0.3, 0.25, 0.35,
"B", "Men", "Z", -0.4, -0.5, -0.3,
"B", "Men", "AA", -0.45, -0.5, -0.4,
"A", "Women", "Z", 0.4, 0.3, 0.5,
"A", "Women", "AA", 0.6, 0.55, 0.65,
"B", "Women", "Z", -0.1, -0.2, 0,
"B", "Women", "AA", -0.3, -0.4, -0.2
)
df %>%
ggplot(aes(x=est, y=var, color=outcome)) +
geom_point(position = position_dodge2(width = 1.0)) +
scale_y_discrete(limits=rev, expand=c(0, 0)) +
scale_color_viridis_d() +
geom_linerange(aes(xmin=l95, xmax=u95),
position = position_dodge2(width=1.0)) +
geom_vline(xintercept = 0, linetype = "dashed") +
facet_grid(~ gender) +
theme_minimal() +
theme(plot.title.position = "plot",
plot.title = element_text(face="bold"),
legend.position = "top",
strip.text = element_text(face="bold")) +
labs(y = NULL,
x = "Standardized Beta",
title = "My cool plot shows this thing",
subtitle = "More important details, 95% confidence intervals",
caption = "Even more important details")
由 reprex package 创建于 2021-06-03 (v2.0.0)
最佳答案
我想我会在 gender
和 var
上分面,outcome
用于实际的 y轴。在我看来,每个面板周围的小灰线也提高了清晰度:
df %>%
ggplot(aes(x=est, y=outcome, color=outcome)) +
geom_point(position = position_dodge2(width = 1.0)) +
scale_y_discrete(limits=rev, expand=c(0, 0)) +
scale_color_viridis_d() +
geom_linerange(aes(xmin=l95, xmax=u95),
position = position_dodge2(width=1.0)) +
geom_vline(xintercept = 0, linetype = "dashed") +
facet_grid(var ~ gender, switch = "y") +
theme_minimal() +
theme(plot.title.position = "plot",
plot.title = element_text(face="bold"),
legend.position = "top",
strip.placement = "outside",
strip.text = element_text(face="bold"),
panel.background = element_rect(color = "gray50")) +
labs(y = NULL,
x = "Standardized Beta",
title = "My cool plot shows this thing",
subtitle = "More important details, 95% confidence intervals",
caption = "Even more important details")
由 reprex package 创建于 2021-06-02 (v0.3.0)
关于r - 如何添加 ggplot2 网格线或颜色以按变量(y 轴)显示多个绘图点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67812857/