teunbrand / ggh4x

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

Custom y axis breaks of facet_wrap2 #71

Closed showteeth closed 1 year ago

showteeth commented 1 year ago

Is it possible to custom y axis breaks of every facet with facet_wrap2?

For example, the follow codes will generate three facets with three different y breaks (facet 4 with breaks from 1.5 to 3.0, facet 6 with breaks from 2.75 to 3.5, facet 8 with breaks from 3.5 to 5.5)

ggplot( mtcars , aes(x=mpg, y=wt, color=as.factor(cyl) )) +
  geom_point(size=3) +
  facet_wrap2(~cyl,scales = "free_y") +
  theme(legend.position="none")

plot

And I want to custom y axis breaks of every facet, the following is codes to custom a single one:

p = ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point(size=3) +
  theme_classic()
ymax <- ceiling(max(mtcars$wt))
p + scale_y_continuous(breaks = c(0, ymax), expand = c(0, 0)) + expand_limits(y = ymax)

plot2

As Waldi suggested in Custom y axis breaks of facet_wrap, this can be solved with facetscales, can this be done with ggh4x?

teunbrand commented 1 year ago

Hi there,

No, facet_wrap2() isn't designed for that. However, facetted_pos_scales() is designed for exactly this, see also here. I've taken the liberty to write an answer to your SO question.

showteeth commented 1 year ago

Thanks for your help!