Home > Net >  How to color specific rows of facet_grid()
How to color specific rows of facet_grid()

Time:12-03

The way I have my data and figure set up now, I'm having a hard time choosing the different colors by model_type. I can get them to have different colors fairly easily by passing color = model_type to aes, but how/where can I actually specify which colors I want?

And yes, I know, there's definitely a better way of naming the facet rows and columns than the hacky way I have it set up now. But it works fairly well for everything except me trying to choose my colors.

Code:

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"),
)

Data:

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"))

CodePudding user response:

You need to define model_type as a factor/character and add

  scale_color_manual(values = vector_with_your_colors)

Code

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

enter image description here

  • Related