Home > front end >  Hide specific facet axis labels in ggplot
Hide specific facet axis labels in ggplot

Time:01-14

you'll see with the code below that I end up with a nicely faceted plot that looks how I need it, but all I want is to hide the y axis labels for all facets except the ones on the far left. So hide labels for facet 2, 3, 4, 6, and 7. That way I am just left with "White", "Black", and "Hispanic" on the far left of each row (I can clean up the prefix_ later). Any ideas?

d2 %>% 
ggplot(., aes(x = var_new, y = coef,
              ymin = ci_lower, ymax = ci_upper))  
  geom_point(color = "red")  
  geom_errorbar(width = 0,
                size = 1,
                color = "red")  
  facet_wrap(~model,
             nrow = 2,
             scales = "free")  
  geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = .3)  
  coord_flip()  
  theme_minimal(base_size = 10)  
  theme(legend.position = "none")
structure(list(model = c(7, 6, 5, 7, 6, 5, 7, 6, 5, 4, 3, 4, 
3, 4, 3, 2, 1, 2, 1, 2, 1), race = c("hispanic", "hispanic", 
"hispanic", "black", "black", "black", "white", "white", "white", 
"hispanic", "hispanic", "black", "black", "white", "white", "hispanic", 
"hispanic", "black", "black", "white", "white"), var_new = c("ela_hispanic", 
"math_hispanic", "sci_hispanic", "ela_black", "math_black", "sci_black", 
"ela_white", "math_white", "sci_white", "after_hispanic", "before_hispanic", 
"after_black", "before_black", "after_white", "before_white", 
"part_hispanic", "full_hispanic", "part_black", "full_black", 
"part_white", "full_white"), coef = c(0.91, 0.2615005, -0.0622102, 
3.1966945, 0.9665615, 0.4419779, -4.1608082, -1.75, -3.4185874, 
-1.72661788, -1.87514649, 0.61605887, 0.58634364, 0.87, 0.4, 
1.52820746, 1.35976557, 1.08885352, 0.8323809019, 0.728991331, 
1.53140561), ci_lower = c(0.3, -1.04316665, -1.68479242, -1.0382233, 
-0.70264707, -1.29579134, -12.008101, -3, -6.4522842, -1.9858909, 
-2.10047863, 0.41173674, 0.37007869, -0.3428254, -0.1, 1.21339829, 
1.07813362, 0.778488586, 0.44183285, 0.30081336, 0.98770764), 
    ci_upper = c(1.2, 1.748, 1.560372, 7.4316126, 2.63577, 2.179747, 
    3.6864845, 0.01, -0.3848905, -1.467344828, -1.64981433, 0.8203809961, 
    0.802608596, 0.4, 0.8, 1.8430166, 1.64139752, 1.39921842, 
    1.22292898, 1.15716932, 2.0751036)), row.names = c(NA, -21L
), class = c("tbl_df", "tbl", "data.frame"))

CodePudding user response:

I don't understand why folks continue to switch the x and y axis variables then use coord_flip to put them round the right way. This is confusing, unnecessary, and requires more code. It's best to just put the variables round the right way and keep the coord as-is.

Once that's done, the simplest solution is to put race on the y axis, and change scales to free_x. I've added a border around each panel to make things a bit clearer.

library(tidyverse)

ggplot(d2, aes(y = race, x = coef, xmin = ci_lower, xmax = ci_upper))  
  geom_errorbar(width = 0, linewidth = 1.5, color = "red3", alpha = 0.5)  
  geom_point(shape = 21, fill = "red2", size = 3, color = 'white')  
  facet_wrap(~ model, nrow = 2, scales = 'free_x')  
  geom_vline(xintercept = 0, linetype = "dashed", linewidth = 0.3)  
  theme_minimal(base_size = 14)  
  theme(legend.position = "none",
        panel.grid.major.y = element_blank(), 
        panel.border = element_rect(color = 'gray75', fill = NA))

enter image description here

If you want to include the prefixes in the facet titles (since they have a 1:1 correspondence with model), you could use tidyr::separate:

d2 %>% 
  separate(var_new, into = c('model_name', 'race')) %>%
  mutate(model = paste(model, model_name, sep = ' - ')) %>%
  ggplot(aes(y = race, x = coef, xmin = ci_lower, xmax = ci_upper))  
  geom_errorbar(width = 0, linewidth = 1.5, color = "red3", alpha = 0.5)  
  geom_point(shape = 21, fill = "red2", size = 3, color = 'white')  
  facet_wrap(~model, nrow = 2, scales = 'free_x')  
  geom_vline(xintercept = 0, linetype = "dashed", linewidth = 0.3)  
  theme_minimal(base_size = 14)  
  theme(legend.position = "none",
        panel.grid.major.y = element_blank(), 
        panel.border = element_rect(color = 'gray75', fill = NA))

enter image description here


Addendum

To compare coefficients across groups like this, it is normally better to put them all in a single linerange plot (similar to a forest plot). I think this provides a much better visualization that requires less cognitive effort from the reader. This also shows a good use-case for coord_flip, namely when you want a vertical dodge between groups.

d2 %>% 
  separate(var_new, into = c('model_name', 'race')) %>%
  mutate(model = paste0('Model ', model, ' : ', model_name)) %>%
  ggplot(aes(x = model, y = coef, ymin = ci_lower, ymax = ci_upper,
             color = race))  
  annotate("segment", y = rep(-Inf, 3), yend = rep(Inf, 3),
           x = c('Model 2 : part', 'Model 4 : after', 'Model 6 : math'), 
           xend =  c('Model 2 : part', 'Model 4 : after', 'Model 6 : math'), 
           linewidth = 22, alpha = 0.05)  
  coord_flip()  
  geom_errorbar(width = 0, linewidth = 1, alpha = 0.5, 
                position = position_dodge(width = 0.5))  
  geom_point(size = 1.5, position = position_dodge(width = 0.5))  
  geom_hline(yintercept = 0, linetype = "dashed", color = "black",
             linewidth = 0.3)  
  scale_color_brewer(palette = 'Set1')  
  theme_minimal(base_size = 14)  
  guides(color = guide_legend(reverse = TRUE))  
  theme(panel.grid.major.y = element_blank(), 
        panel.border = element_rect(color = 'gray75', fill = NA),
        axis.text.y = element_text(hjust = 0))

enter image description here

  • Related