Skip to content

guide_axis_nested() : pad_discrete=0 #73

@DanChaltiel

Description

@DanChaltiel

Hi,

Thank you for this incredible package.

I'm building a rather large plot with a nested categorical x-axis.
My X labels are long and tend to overwrite each other, so I want to add a 30°angle on the top level.
However, this caused several alignment problems I couldn't solve using the available documentation.

library(tidyverse)
library(legendry)

p = mpg |>
  filter(class %in% c("compact", "minivan")) |>
  mutate(cyl = str_replace(cyl, "4", "four"),
         cyl = str_replace(cyl, "5", "five"),
         cyl = str_replace(cyl, "6", "six"),
         cyl = str_replace(cyl, "8", "eight")) |>
  ggplot() +
  aes(interaction(cyl, class), hwy) +
  geom_boxplot() +
  labs(x=NULL)

Default behaviour (for comparison)

This is very nice!

p +
  guides(
    x = legendry::guide_axis_nested()
  )

Image

Angle on top level

With an angle on the top level, the alignment is OK, but the brackets shrink due to pad_discrete=0.
Unfortunately, removing it causes the alignment to be very bad.

levels_text = list(element_text(angle = 30, hjust = 1, vjust = 1), 
                   element_text(angle = 0, hjust = 0.5, vjust = 1))
p +
  guides(
    x = legendry::guide_axis_nested(levels_text=levels_text, pad_discrete=0)
  )

Image

Is there a way to get the best of the two worlds?

My first guess would be to state pad_discrete=c(0, NA) to have 0 for the top level and waiver() for the bottom level.

PS: I couldn't get reprex to work with legendry, I get crashes every times.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions