Home > front end >  Order the coefficients in specific rows of facet_grid
Order the coefficients in specific rows of facet_grid

Time:12-04

I am using facet_grid() to display a 2x2 of different combinations of model types for racial groups and levels of participation in a program.

By using scales = "free" I am able to separate out the y axes for each row and only display the relevant coefficients. But, how can I then specify the model/variable order within each panel row? Typically, I would do something like:

model_order <- c("White", "Black", "Hispanic")

And then pass that through to scale_x_discrete(). (And would have High, then Medium, then Low in that order).

But that does not seem to work in this case because of using scales = "free". Is there a workaround for controlling the order?

Code:

mylabels <- c("1" = "Linear",
                       "2" = "Logit",
                       "3" = "Race",
                       "4" = "Level")

ggplot(dx, aes(x = var, y = coef,
       ymin = ci_lower, ymax = ci_upper))  
  geom_point(size = 2)  
  geom_errorbar(width = 0.1,
                size = 1)  
  facet_grid(effect~model,
             scales = "free",
             labeller = as_labeller(mylabels))   
  scale_y_continuous(breaks = seq(-3, 3, by = 1))  
  coord_flip()  
  theme_bw(base_size = 15)  
  theme(legend.position = "none")

Data:

structure(list(var = c("White", "Black", "Hispanic", "White", 
"Black", "Hispanic", "High", "Medium", "Low", "High", "Medium", 
"Low"), coef = c(1.64, 1.2, 0.4, 1.45, 0.17, 0.6, 1.04, 0.05, 
-0.74, -0.99, -0.45, -0.3045), ci_lower = c(1.3, 0.86, 0.06, 
1.11, -0.17, 0.26, 0.7, -0.29, -1.08, -1.33, -0.79, -0.6445), 
    ci_upper = c(1.98, 1.54, 0.74, 1.79, 0.51, 0.94, 1.38, 0.39, 
    -0.4, -0.65, -0.11, 0.0355), model = c(1, 1, 1, 2, 2, 2, 
    1, 1, 1, 2, 2, 2), effect = c(3, 3, 3, 3, 3, 3, 4, 4, 4, 
    4, 4, 4)), 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")), effect = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1L), class = "col_spec"))

CodePudding user response:

You can define your variable as a factor, and then reorder their levels:

library(dplyr)
library(ggplot2)

mylabels <- c("1" = "Linear",
              "2" = "Logit",
              "3" = "Race",
              "4" = "Level")
dx %>% 
  mutate(var = forcats::fct_relevel(var,"High","Medium")) %>%
  ggplot(aes(x = var, y = coef,
                 ymin = ci_lower, ymax = ci_upper))  
  geom_point(size = 2)  
  geom_errorbar(width = 0.1,
                size = 1)  
  facet_grid(effect~model,
             scales = "free",
             labeller = as_labeller(mylabels))   
  scale_y_continuous(breaks = seq(-3, 3, by = 1))  
  coord_flip()  
  theme_bw(base_size = 15)  
  theme(legend.position = "none")
  • Related