Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom breaks with facetted_pos_scales returns error #91

Closed
EcologyTom opened this issue Feb 7, 2023 · 5 comments
Closed

Custom breaks with facetted_pos_scales returns error #91

EcologyTom opened this issue Feb 7, 2023 · 5 comments

Comments

@EcologyTom
Copy link

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

EcologyTom commented Feb 8, 2023

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
Copy link
Owner

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
Copy link
Owner

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

teunbrand added a commit that referenced this issue Apr 4, 2023
* Attempt to update pkgdown part 2

* Drop dependency on digest

* Adapt to upcoming ggplot2 #95

* Better message in facetted_pos_scales #91

* try_require() -> check_installed()

* Use {cli} for messages

* Add `inv` option

* Fix #97

* Smarter omission of fixed scales

* Better removal of spurious whitespace

* Add scale_{x/y}_manual

* Polish news

* Mark scale_{x/y}_manual as experimental

* Last polishes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants