Skip to content

When setting "scales" in facet_grid, the horizontal justification of the axis labels shifts among the panels #5820

@ujtwr

Description

@ujtwr

Case1

Panel "F1" has 3 values and Panel "F2" has other 3 values.
We use facet_grid without "scale" option, Axis Y of both panels has same 6 values. So, if we set "hjust = 0" in "element_text" on "axis.text.y", Text of Y axis are aligned left.

tibble(
  F = c(rep("F1", 3), rep("F2", 3)),
  Y = c("AAAAAAAAAAAAAAAAAAA", "BBBBB", "CCCCCCCCC", "DDDDD", "EEEEEEEE", "FFF"),
  Cnt = c(10, 20, 30, 50, 40 ,60)
) %>% 
  ggplot(mapping = aes(y = Y, x = Cnt)) +
  geom_col() +
  facet_grid(
    rows = vars(F)
  ) +
  theme(
    axis.text.y = element_text(hjust = 0)
  )

Rplot

Case2

Next, we add an option "scales = free_y" on facet_grid function, panel F1 and F2 has other values that have different width. So, the alignment position of the text varies depending on the panel.

tibble(
  F = c(rep("F1", 3), rep("F2", 3)),
  Y = c("AAAAAAAAAAAAAAAAAAA", "BBBBB", "CCCCCCCCC", "DDDDD", "EEEEEEEE", "FFF"),
  Cnt = c(10, 20, 30, 50, 40 ,60)
) %>% 
  ggplot(mapping = aes(y = Y, x = Cnt)) +
  geom_col() +
  facet_grid(
    rows = vars(F),
    scales = "free_y"
  ) +
  theme(
    axis.text.y = element_text(hjust = 0)
  )

Rplot02

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions