簡體   English   中英

如何為 facet_grid() 的特定行着色

[英]How to color specific rows of facet_grid()

我現在設置數據和圖形的方式,我很難通過 model_type 選擇不同的 colors。 通過將color = model_type傳遞給 aes,我可以很容易地讓他們擁有不同的 colors,但是我如何/在哪里可以實際指定我想要哪個 colors?

是的,我知道,肯定有比我現在設置的那種老掉牙的方式更好的命名構面行和列的方式。 但除了我試圖選擇我的 colors 之外,它對所有事情都非常有效。

代碼:

mylabels <- c("1" = "Springfield",
              "2" = "Valley",
              "3" = "Glenside",
              "4" = "Upper",
              "5" = "Linear",
              "6" = "Logit")

ggplot(dz, aes(x = var, y = coef, color = model_type,
               ymin = ci_lower, ymax = ci_upper)) +
  geom_point() +
  geom_errorbar(width = 0.1) +
  facet_grid(model_type~school_num, labeller = as_labeller(mylabels)) +
  coord_flip() +
  theme_bw(base_size = 14) +
  theme(legend.position ="none",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill = "white"),
)

數據:

structure(list(var = c("teacher_m", "test_score", "teacher_m", 
"test_score", "teacher_m", "test_score", "teacher_m", "test_score", 
"teacher_m", "test_score", "teacher_m", "test_score", "teacher_m", 
"test_score", "teacher_m", "test_score"), coef = c(1.1, 0.92, 
0.5, 0.45, 0.5, 0.47, 0.32, 0.5, 0.4, 0.68, 0.64, 0.25, 0.53, 
0.26, 1.04, 0.62), ci_lower = c(0.99, 0.81, 0.33, 0.34, 0.34, 
0.35, 0.21, 0.39, 0.29, 0.57, 0.44, 0.1, 0.42, 0.15, 0.93, 0.51
), ci_upper = c(1.21, 1.03, 0.61, 0.56, 0.61, 0.58, 0.43, 0.61, 
0.51, 0.79, 0.75, 0.4, 0.6, 0.4, 1.06, 0.73), school_num = c(1, 
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4), model_type = c(5, 
5, 6, 6, 5, 5, 6, 6, 5, 5, 6, 6, 5, 5, 6, 6)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L), 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")), school_num = structure(list(), class = c("collector_double", 
    "collector")), model_type = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

您需要將model_type定義為一個因素/字符並添加

+ scale_color_manual(values = vector_with_your_colors)

代碼

ggplot(df, aes(x = var, y = coef, color = factor(model_type),
               ymin = ci_lower, ymax = ci_upper)) +
  geom_point() +
  geom_errorbar(width = 0.1) +
  facet_grid(model_type~school_num, labeller = as_labeller(mylabels)) +
  coord_flip() +
  theme_bw(base_size = 14) +
  theme(#legend.position ="none",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill = "white"),
  )+
 scale_color_manual(values = c("5" = "red", "6" = "blue"))

Output

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM