使用 facet_wrap() 仅更改一个面板的点颜色

Changing point colors for only one panel using facet_wrap()

我正在尝试使用 facet_wrap() 绘制一堆模型结果,并希望将“ses”的点和误差条的颜色更改为红色,但仅适用于面板 3。到目前为止,我还没有我很想知道如何成功地做到这一点。有什么想法吗?

这是一些(非常简短的)示例代码和数据:

代码:

ggplot(d, aes(x = var, y = coef,
              ymin = ci_lower, ymax = ci_upper,
              color = var)) +
  geom_point() +
  geom_errorbar(width = .1) +
  facet_wrap(~model, nrow = 1) +
  labs(title = "Model results",
       y = "Coefficient",
       x = "") +
  geom_hline(yintercept = 0, linetype = "solid", color = "black", size = .5) +
  coord_flip() +
  scale_color_manual(values = c("white" = "blue",
                                "score" = "black",
                                "female" = "black",
                                "ses" = "black")) +
  theme_bw(base_size = 12)

数据:

structure(list(var = c("white", "score", "female", "ses", "white", 
"score", "female", "ses", "white", "score", "female", "ses"), 
    coef = c(0.7, 0.5, 0.6, -0.4, 0.2, 1.02, 0.1, -0.1, 0.3, 
    0.4, 0.5, 0.2), ci_lower = c(0.4, 0.05, 0.3, -0.7, -0.1, 
    0.52, -0.2, -0.4, -0.1, 0.1, 0.2, -0.1), ci_upper = c(1, 
    0.9, 0.9, -0.1, 0.5, 1.4, 0.4, 0.2, 0.6, 0.7, 0.8, 0.5), 
    model = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
    cols = list(var = structure(list(), class = c("collector_character", 
    "collector")), coef = structure(list(), class = c("collector_double", 
    "collector")), ci_lower = structure(list(), class = c("collector_double", 
    "collector")), ci_upper = structure(list(), class = c("collector_double", 
    "collector")), model = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

一个选项是重新编码您的 var 列,您在其中为 var="ses"model=3 分配不同的值,这将允许您为“ses”分配红色第 3 组:

library(ggplot2)

d$color <- ifelse(d$var == "ses" & d$model == 3, "ses_red", d$var)

ggplot(d, aes(x = var, y = coef,
              ymin = ci_lower, ymax = ci_upper,
              color = color)) +
  geom_point() +
  geom_errorbar(width = .1) +
  facet_wrap(~model, nrow = 1) +
  labs(title = "Model results",
       y = "Coefficient",
       x = "") +
  geom_hline(yintercept = 0, linetype = "solid", color = "black", size = .5) +
  coord_flip() +
  scale_color_manual(values = c("white" = "blue",
                                "score" = "black",
                                "female" = "black",
                                "ses" = "black",
                                "ses_red" = "red")) +
  theme_bw(base_size = 12)

根据您想要的结果,第二种选择是拆分数据集并使用多层:

library(ggplot2)
library(dplyr)

d1 <- d |> 
  filter(!(var == "ses" & model == 3))

d2 <- d |> 
  filter(var == "ses" & model == 3)

ggplot(d1, aes(x = var, y = coef,
              ymin = ci_lower, ymax = ci_upper,
              color = var)) +
  geom_point() +
  geom_errorbar(width = .1) +
  geom_point(data = d2, color = "red") +
  geom_errorbar(data = d2, color = "red", width = .1) +
  facet_wrap(~model, nrow = 1) +
  labs(title = "Model results",
       y = "Coefficient",
       x = "") +
  geom_hline(yintercept = 0, linetype = "solid", color = "black", size = .5) +
  coord_flip() +
  scale_color_manual(values = c("white" = "blue",
                                "score" = "black",
                                "female" = "black",
                                "ses" = "black")) +
  theme_bw(base_size = 12)