teunbrand / ggh4x

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

facet_nested incompatible with facetted_pos_scales? #134

Closed tombradferlawrence closed 10 months ago

tombradferlawrence commented 10 months ago

Thanks for this excellent package. I have a facet_grid2 plot with custom x axes set using facetted_pos_scales. I would also like to use nested facets. When I set more than one variable in the facet command (using either var( ) or a formula), then facetted_pos_scalesis ignored, or at least the axes breaks and labels are no longer in the correct locations. Is there some sort of conflict between facet_nestedand facetted_pos_scales? Or do I have to alter the way I define the custom breaks to match the nested facets?

Any suggestions for ways I could resolve this would be most appreciated. Thanks

teunbrand commented 10 months ago

There shouldn't be a conflict (not saying that there isn't). Could you provide some example with one of the built-in datasets of where this approach fails?

tombradferlawrence commented 10 months ago

Thanks for such a prompt response. Here's some code. I might have been able to narrow down this issue. With this example it looks like the breaks are being set, but the limits are not being recognised. If I don't set any limits, the axes are restricted to the values in the data, and so there are no axes values if they aren't in that range. If I set only one of the limits and use 'NA' in the other, then that is ignored.

So, it looks like I need to manually set both the limits for this to work. Perhaps that is how the function is supposed to behave. Sorry if this is the case and I just didn't read the documentation correctly!

# Subet some example data
DATA <- mpg[mpg$manufacturer %in% c("dodge", "ford") & mpg$class %in% c("pickup", "suv"),]

# Set custom x axes breaks
CUSTOM.BREAKS <- list(
  scale_x_continuous(breaks = c(5, 6, 17, 23)), # Top left plot, will only plot data, no axes values
  scale_x_continuous(breaks = c(0, 10, 15, 20, 25), limits = c(0, NA)), # Top right plot, 
                                                                        honours the lower limit, 
                                                                        so shows '0' and '10' on the axes, but not 15+
  scale_x_continuous(breaks = c(0, 10, 15, 20, 25), limits = c(0, 25)), # Second from top on left, 
                                                                        shows full range of values.
  scale_x_continuous(breaks = c(5, 6, 17, 23)),
  scale_x_continuous(breaks = c(10, 20, 30)),
  scale_x_continuous(breaks = c(9, 10, 11, 12, 13, 14, 15, 16, 17, 18)),
  scale_x_continuous(breaks = c(11, 22, 33)),
  scale_x_continuous(breaks = c(8, 16, 24))
)

# Set strip theme
PLOT.STRIPS <- strip_nested(
  text_y = elem_list_text(
    colour = c(rep("white", 2), rep("black", 4))),
  background_y = elem_list_rect(
    fill = c("#210c4a", "#8a226a", rep(c("#bc3754", "#f9cb35"), 2))),
  by_layer_y = FALSE
)

# Plot
ggplot(DATA, aes(cty, hwy)) +
  geom_point() +
  facet_grid2(year + class ~ manufacturer, scales = "free", independent = "x", axes = "y", 
                         remove_labels = "y", strip = PLOT.STRIPS) +
  facetted_pos_scales(x = CUSTOM.BREAKS)
tombradferlawrence commented 10 months ago

And after some more testing with my real data, I realise that nesting the facets will also change the plotting order I was expecting when setting the custom breaks.

Going to close this now. So sorry I posted before I realised I was doing something wrong. The package is working brilliantly.

teunbrand commented 10 months ago

Alright thanks for the interest anyway! Making examples strikes again as an excellent bugfix :)