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 breaks with facetted_pos_scales returns error #91

Closed EcologyTom closed 1 year ago

EcologyTom commented 1 year ago

Hi,

Many thanks for ggh4x, it's great. I want to create custom breaks in a 40 panel plot I've created with facet_grid2. Each panel needs custom x breaks. Following an answer on SO I created a list with my custom breaks like so:

CUSTOM.BREAKS <- list(
  scale_x_continuous(breaks = c(40, 80, 120, 160)),
  scale_x_continuous(breaks = c(50, 100, 150, 200, 250)),
  scale_x_continuous(breaks = c(40, 80, 120, 160)),
  scale_x_continuous(breaks = c(50, 100, 150, 200, 250)),
  scale_x_continuous(breaks = c(0, 10, 20, 30)),
  scale_x_continuous(breaks = c(0, 10, 20, 30)),
etc, one custom scale per panel

And refer to the list in the ggplot call: + facetted_pos_scales(y = CUSTOM.BREAKS) but this returns:

Error in facetted_pos_scales(y = CUSTOM.BREAKS) : 
  Invalid facetted scale specifications.

I must be referring to the panels incorrectly. I'm unclear how I could refer to the individual panels directly in facetted_pos_scales, the help file refers to only rows or columns. Please could you give some direction on how to set the breaks for each the indivudal panel? Many thanks.

EcologyTom commented 1 year ago

Oh, I'm sorry. I looked again at the documentation and realised I should be using facetted_pos_scales(x = CUSTOM.BREAKS) because I'm changing the x axis.

teunbrand commented 1 year ago

Yep, that's right. Perhaps the error message should be a little more helpful on this. There is also scale_x_facet() to do a similar thing without bothering with lists.

EcologyTom commented 1 year ago

Thanks. Perhaps a slight edit to the manual text for facetted_pos_scales might help? Just my opinion of course, but to me "axes" would be more intuitive than "position" in this context.

I also noticed that even when I set the breaks, sometimes the first or last values are missing. Is it possible to force a tick mark to show at 0, for example?

teunbrand commented 1 year ago

Yeah that should be possible by including 0 in the limits. If you have no negative values, you can use limits = c(0, NA). If you do have negative values and you want to include 0, you can use limits = ~ c(pmin(.x[1], 0), .x[2]).

While 'axes' sound more intuitive, 'position scales' as a term in ggplot2 is what is really being altered, so I'm not keen on changing the terms away from what ggplot2 uses.

teunbrand commented 1 year ago

Error should now read more informative text:

devtools::load_all("~/packages/ggh4x")
#> ℹ Loading ggh4x
#> Loading required package: ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facetted_pos_scales(x = list(scale_y_continuous()))
#> Error in `facetted_pos_scales()`:
#> ! The `x` argument should be "NULL", or a list of formulas and/or
#>   position scales with the x aesthetic.

Created on 2023-03-28 with reprex v2.0.2