Remove or combine headers and adjust distance in ggplot2?

Viewed 117

I want to make a few edits to a ggplot2 plot but I'm stuck.

For the headers, I want to combine the "Substance" labels (aka just one single header for "Alcohol", "Cigarettes" etc.) and then I remove the 0's and the 1's at the top entirely, since they're already labeled via the legend and are thus redundant.

I also want to move the markers of the same shape slightly closer together (aka each triangle should be closer to the other triangle, and same with the circles). That way it'll be easier to tell that they're supposed to be grouped together.

Finally, I want to remove the legend markers under "Color" and make the color of the word "Contemporaneous" gray to correspond with the figure. That way they can't be mixed up with the shape legend indicators.

I've tried strip.text.x = element_blank() and a few other things but keep getting errors. Thank you!

Here's the figure I have created:

plot

And here's my code:

ggplot(data = gfrdata, 
       aes(x = factor(model_cont0_lag1), 
           y = est, 
           ymin = lcl, 
           ymax = ucl, 
           color = model_cont0_lag1, 
           shape = model_year0_curr1)) + 
  geom_pointrange(size = 0.8) +
  ylim(-24, 20) +
  scale_color_manual(values = c(Contemporaneous = "gray60", Lagged = "gray0")) +
  labs(color = 'Color', shape = 'Shape', x = ' ', y = 'Percent Difference (95% CI)') +
  geom_errorbar(aes(ymin = lcl, ymax = ucl), width = 0.45, cex = 1) + 
  facet_grid(.~substance+model_year0_curr1, scale = 'free', space = 'free') +
  geom_hline(yintercept = 0, linetype = 2) +
  theme_classic() +
  theme(panel.spacing=unit(0,"lines"),
        axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        strip.background = element_blank(),
        legend.position = "right", legend.direction = "vertical",
        legend.text = element_text(color = "black"), 
        legend.title = element_text(face = "bold"))```
1 Answers

Maybe this is what you are looking for:

  1. To get only one label for the facets my approach facets by substance only whereby I switched to facet_wrap.
  2. To get the order of the pointranges right I map model_year0_curr1 on x and make use of position_dodge in both geom_pointrange and geom_errorbar.
  3. Using position_dodge allows to set the width to reduce the spacing between circles and triangels
  4. To get the colors of the legend labels right I make use of ggtext::element_markdown which allows to sytle the legend labels using some CSS.

Using some random example data try this:

library(ggplot2)
library(ggtext)

set.seed(42)
y <- runif(16, min = -.1, max = .1) * 100

gfrdata <- data.frame(
  substance = rep(c("Alcohol", "Cigarettes", "E - cigarettes", "Marijuana"), each = 4),
  model_year0_curr1 = rep(c("Past 30 Days", "Previous Year"), each = 2),
  model_cont0_lag1 = c("Contemporaneous", "Lagged"),
  est = y,
  lcl = y - runif(16, max = .05) * 100,
  ucl = y + runif(16, max = .05) * 100
)

ggplot(data = gfrdata, 
       aes(x = rev(model_year0_curr1), 
           y = est, 
           ymin = lcl, 
           ymax = ucl, 
           color = model_cont0_lag1, 
           shape = model_year0_curr1)) + 
  geom_pointrange(size = 0.8, position = position_dodge(0.5)) +
  ylim(-24, 20) +
  scale_color_manual(values = c(Contemporaneous = "gray60", Lagged = "gray0"), 
                     labels = c(Contemporaneous = "<span style='color: grey60'>Contemporaneous</span>",
                                Lagged = "<span style='color: grey0'>Lagged</span>")) +
  labs(color = 'Color', shape = 'Shape', x = ' ', y = 'Percent Difference (95% CI)') +
  geom_errorbar(aes(ymin = lcl, ymax = ucl), position = position_dodge(0.5), width = 0.45, cex = 1) + 
  facet_wrap(~substance, nrow = 1) +
  geom_hline(yintercept = 0, linetype = 2) +
  theme_classic() +
  theme(panel.spacing=unit(0,"lines"),
        axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        strip.background = element_blank(),
        legend.position = "right", legend.direction = "vertical",
        legend.text = element_markdown(color = "black"), 
        legend.title = element_text(face = "bold"))

Related