Home > database >  Centering all facets on 0 in ggplot with facet_wrap
Centering all facets on 0 in ggplot with facet_wrap

Time:01-25

I have what is hopefully a simple question.

I am using facet_wrap() to stack 3 plots on top of each other.

I am hoping to get all 3 plots centered at 0, so the hline all are directly above each other. Ideally while also allowing their scales to range freely as needed along the x axis (which I am already doing with scales = "free").

Any ideas? See example code and data below.

d_example %>% 
  ggplot(aes(x = var, y = coef, 
             ymin = ci_lower, ymax = ci_upper,
             color = race))  
  geom_point(position = position_dodge(width = 0.7),
             size = 3)  
  geom_errorbar(width = 0, 
                size = 1.1, 
                alpha = 0.6, 
                position = position_dodge(width = 0.7))  
  facet_wrap(~ outcome, 
             nrow = 3,
             scales = "free")  
  coord_flip()  
  geom_hline(yintercept = 0, 
             linetype = "dashed", 
             color = "black",
             size = .3)  
  theme_minimal()
d_example <- structure(list(var = c("score_A", "score_B", "score_C", "score_A", 
"score_B", "score_C", "score_A", "score_B", "score_C", "friends_A", 
"friends_B", "friends_A", "friends_B", "friends_A", "friends_B", 
"poverty_A", "poverty_B", "poverty_A", "poverty_B", "poverty_A", 
"poverty_B"), coef = c(-0.8, -0.7, 0.12, -0.7, -0.9, -0.05, -0.73, 
-1, 0.02, 0.55, -0.09, 0.51, 0.2, 0.65, 0.05, 1.8, 0.34, 1.103, 
0.077, 1.02, 0.06), race = c("white", "white", "white", "black", 
"black", "black", "hispanic", "hispanic", "hispanic", "white", 
"white", "black", "black", "hispanic", "hispanic", "white", "white", 
"black", "black", "hispanic", "hispanic"), outcome = c("score", 
"score", "score", "score", "score", "score", "score", "score", 
"score", "friends", "friends", "friends", "friends", "friends", 
"friends", "ses", "ses", "ses", "ses", "ses", "ses"), ci_lower = c(-1, 
-0.9, -0.08, -0.9, -1.1, -0.25, -0.93, -1.2, -0.18, 0.35, -0.29, 
0.31, 0, 0.45, -0.15, 1.6, 0.14, 0.903, -0.123, 0.82, -0.14), 
    ci_upper = c(-0.6, -0.5, 0.32, -0.5, -0.7, 0.15, -0.53, -0.8, 
    0.22, 0.75, 0.11, 0.71, 0.4, 0.85, 0.25, 2, 0.54, 1.303, 
    0.277, 1.22, 0.26)), class = c("spec_tbl_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -21L), spec = structure(list(
    cols = list(var = structure(list(), class = c("collector_character", 
    "collector")), coef = structure(list(), class = c("collector_double", 
    "collector")), race = structure(list(), class = c("collector_character", 
    "collector")), outcome = structure(list(), class = c("collector_character", 
    "collector")), ci_lower = structure(list(), class = c("collector_double", 
    "collector")), ci_upper = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

CodePudding user response:

Here's a hacky but simple answer that gives each facet a free range, while keeping zero in the center:

Add an invisible layer with reversed y's, such that every facet will be naturally symmetrical around 0.

geom_errorbar(aes(y = -coef, ymin = -ci_lower, ymax = -ci_upper),
              alpha = 0,
              position = position_dodge(width = 0.7))  

enter image description here

CodePudding user response:

I'm adding scale_y_continuous(limits=) below; this allows easy alignment of the hlines:

d_example %>%
  ggplot(aes(x = var, y = coef,
             ymin = ci_lower, ymax = ci_upper,
             color = race))  
  geom_point(position = position_dodge(width = 0.7),
             size = 3)  
  geom_errorbar(width = 0,
                size = 1.1,
                alpha = 0.6,
                position = position_dodge(width = 0.7))  
  facet_wrap(~ outcome,
             nrow = 3,
             scales = "free")  
  coord_flip()  
  geom_hline(yintercept = 0,
             linetype = "dashed",
             color = "black",
             linewidth = .3)  
  scale_y_continuous(limits = range(unlist(d_example[,c("coef","ci_lower","ci_upper")])))  
  theme_minimal()

enter image description here

If you want truly-centered, use this instead:

scale_y_continuous(limits = c(-1,1) * max(abs(range(unlist(d_example[,c("coef","ci_lower","ci_upper")])))))
  • Related