teunbrand / ggh4x

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

feature request or Q&A: compatibility between facetted_pos_scales and scale_(x-y)_break #64

Closed andresdlrn closed 2 years ago

andresdlrn commented 2 years ago

First of all.. THANKS! the features that ggh4x add to ggplot2 are amazing and really helpfull. As I said above, this is a feature request or Q&A. I'm trying to add an axis break (using scale_x_break, of ggbreak package) on a facetted ggplot and isn't possible due to "_Error in validate_facettedscale(y, "y") : RHS of formula does not result in appropriate scale." I've been looking through the web and haven't found if it's possible yet, I asume that the function is only compatible with scale_continuous and scale_x_discrete. So, here's the question/idea: is there a way to do an axis break using facetted_pos_scales? Thanks again! Andrés

teunbrand commented 2 years ago

Hi there,

I don't think it is possible to use facetted_pos_scales() with the {ggbreak} package. As the error message indicates, the ggbreak::scale_x_break() function does not return a scale in the typical sense. The ggplot2 extension system is setup such that all scale objects inherit from the Scale ggproto class, which facetted_pos_scales() can work with. It shouldn't have too much trouble with scales from extension packages as long as they follow the path that the ggplot2 developers have blazed. However, ggbreak::scale_x_break() seems to circumvent working with this class directly, which subsequently violates all kinds of assumptions in facetted_pos_scales().

library(ggplot2)
library(ggbreak)

x <- scale_x_continuous()
inherits(x, "Scale")
#> [1] TRUE

y <- scale_x_break(breaks = c(0, 1))
inherits(y, "Scale")
#> [1] FALSE

Created on 2022-03-11 by the reprex package (v2.0.1)

andresdlrn commented 2 years ago

Appreciate the quickness of the answer and clearness of the explanation. Based on what you said ther isn't compatibility between the two packages due to class of the ggbreak object. Besides that, do you know if it's to do what i'm trying to do using ggh4x? I mean, make an axis break using ggplot2 and then use facetted_pos_scales()? Thanks in advance!

teunbrand commented 2 years ago

I don't think so; ggh4x has no functions to do anything with discontinuous position scales. As long as there is no other extension package that implements axis breaks through the Scale ggproto class, I don't think it is possible. If I were in your position, I'd try circumventing facetting altogether and try to get the desired result through plot composition, such as with {patchwork}.

teunbrand commented 2 years ago

I'm going to close this issue, as I'm unlikely to do something with discontinuous axes guides.

smouksassi commented 1 year ago

this is just amazing how all work together like a charm reprex with facet_nested_wrap with strip_split, facetted_pos_scales and force_panelsizes

library(ggplot2)
 library(ggh4x)
 ggplot(mtcars, aes(wt, mpg)) +
   geom_point(aes(colour = as.factor(vs))) +
   geom_smooth(method="lm")+
   facet_nested_wrap(vs~am,scales="free_x",
                     strip = strip_split(position = c("top", "right")),
                     labeller =  label_both)+
   facetted_pos_scales(
     x = list(
       am == 1 ~ scale_x_continuous(),
       am == 0 ~ scale_x_reverse()
     )
   )+
   force_panelsizes( rows = c(2,5))# does not make sense just illustrate
#> `geom_smooth()` using formula 'y ~ x'

Created on 2022-10-24 with reprex v2.0.2

teunbrand commented 1 year ago

Thanks, its the first time I've seen strip_split() used by someone that isn't me, so I'm glad it seems to be working out!