[英]Plot of coefficients + confidence intervals: transparency of point according to logical condition (overlap with 0)
我想要 plot 点 + 置信区间 (CI)。 点和 CI 应遵循用户定义的调色板,但除此之外,点的填充应根据逻辑条件更改。 更详细地说,我的目标是 plot 多个模型的系数 + 置信区间(不同的响应变量,但同一组解释变量)。 因此,我希望我的系数遵循我建立的调色板:基本上,与相同响应相关的系数应该具有相同的颜色。 除了遵循用户定义的调色板之外,我还希望与 0 重叠的系数是透明的(或者我应该说,具有透明填充,但颜色勾勒出圆周)。 CI 的线条应保持不变(与点颜色相同,实心不透明)。 我已经使用虚拟数据框测试了一些选项,尝试使用填充、透明度(alpha)或将颜色存储在数据框本身中,但到目前为止我一直无法达到预期的结果。 非常感谢任何帮助
library(tidyverse)
# user-defined color palette
response.manual.col.scale <- c("#A6761D", "#FC8D62")
# dummy data frame: coefficients + confidence intervals for two different models (different responses, same predictors)
df <- data.frame(response = c("r1", "r1", "r2", "r2"),
explanatory = c("var1", "var2", "var1", "var2"),
coef = c(1, 2, 1, 2),
lower = c(-1, 1, 0.5, -2),
upper = c(4, 3, 1.5, 6)) %>%
mutate(
# variable storing whether the confidence interval overlaps with 0
coef.zero = if_else((lower/upper) < 0, true = TRUE, false = FALSE),
# variable storing the colour to be used for fill and colour
resp.col = rep(c("#A6761D", "#FC8D62"), each = 2),
resp.fill = if_else(coef.zero == TRUE, true = "black", false = resp.col))
ggplot(data = df,
aes(x = explanatory,
y = coef,
ymin = lower,
ymax = upper,
fill = response,
col = response)) +
geom_pointrange(position = position_dodge(width = 0.4), size = 2) +
geom_hline(yintercept = 0) +
scale_fill_manual(values = response.manual.col.scale) +
scale_color_manual(values = response.manual.col.scale) +
theme_bw()
这些是获得我想要的 plot 的一些不成功的尝试。
ggplot(data = df,
aes(x = explanatory,
y = coef,
ymin = lower,
ymax = upper,
fill = if_else(coef.zero == TRUE, true = "transparent", false = response),
col = response)) +
geom_pointrange(position = position_dodge(width = 0.4), size = 2) +
geom_hline(yintercept = 0) +
# scale_fill_manual(values = response.manual.col.scale) +
scale_color_manual(values = response.manual.col.scale) +
theme_bw()
ggplot(data = df,
aes(x = explanatory,
y = coef,
ymin = lower,
ymax = upper,
fill = resp.fill,
col = resp.col)) +
geom_pointrange(position = position_dodge(width = 0.4), size = 2) +
geom_hline(yintercept = 0) +
theme_bw()
ggplot(data = df,
aes(x = explanatory,
y = coef,
ymin = lower,
ymax = upper,
fill = response,
col = response,
# transparency based on condition
alpha = if_else(coef.zero == TRUE, true = 0, false = 1))) +
geom_pointrange(position = position_dodge(width = 0.4), size = 2) +
geom_hline(yintercept = 0) +
scale_fill_manual(values = response.manual.col.scale) +
scale_color_manual(values = response.manual.col.scale) +
theme_bw() +
guides(alpha = "none")
提前谢谢了
如果您要努力在数据框中创建 colors,则可以将它们与身份标度一起使用:
df <- data.frame(response = c("r1", "r1", "r2", "r2"),
explanatory = c("var1", "var2", "var1", "var2"),
coef = c(1, 2, 1, 2),
lower = c(-1, 1, 0.5, -2),
upper = c(4, 3, 1.5, 6)) %>%
mutate(
# variable storing whether the confidence interval overlaps with 0
coef.zero = lower/upper < 0,
# variable storing the colour to be used for fill and colour
resp.col = rep(c("#A6761D", "#FC8D62"), each = 2),
resp.fill = if_else(coef.zero == TRUE, true = "white", false = resp.col))
ggplot(data = df,
aes(x = explanatory,
y = coef,
ymin = lower,
ymax = upper,
fill = resp.fill,
col = resp.col)) +
geom_pointrange(position = position_dodge(width = 0.4), size = 2,
shape = 21) +
geom_hline(yintercept = 0) +
scale_fill_identity() +
scale_color_identity() +
theme_bw()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.