teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

How to add space around the nesting lines in `guide_axis_nested` #132

Closed mlell closed 8 months ago

mlell commented 8 months ago

Thank you for this package which solves an issue I have been suffering from for years! It is almost perfect, despite one thing:

library(tibble)
library(dplyr)
library(ggplot2)
tibble(loc = rep(c("München", "Berlin", "Alteglofsheim"), 2), 
       year = c("2020","2021","2020","2021", "2020", "2021"), 
       y = runif(6,0,1)) %>% mutate(x = paste0(year,".",loc)) %>% 
  ggplot() + aes(x = x, y=y) + geom_col() + 
    scale_x_discrete(guide = ggh4x::guide_axis_nested(inv=TRUE,angle=90))

The text is very close to the nesting line. How can I add some space there?

As a workaround, I could just add some space before the city names, however this only works if the text is vertical. Is there a more canonical way, like a theme() setting? I only found ggh4x.axis.nesttext.x/y which as an element_text() object does not have a margin setting or similar.

teunbrand commented 8 months ago

Every element_text() has its own margin, so you could use theme(axis.text.x.bottom = element_text(margin = margin(t = 2.2, b = 10))) to space the nesting line further from the rotated text.

mlell commented 8 months ago

Ah, yes I did not think of modifying the text instead of the line. Thanks!