Closed yjunechoe closed 2 years ago
Hi there,
Thanks for letting me know!
I think the underlying issue in this case is the typical constraint of grid-style facets that scales cannot vary within rows and columns. You could use independent
argument in facet_grid2()
to relieve this constraint, but that gives the wrong interpretation of the plot if you hide breaks and labels, as one would assume that the breaks on the left y-axis also apply to the right panel (which they do not, which you can see if you remove the guide in the plot below).
library(ggh4x)
library(dplyr)
base <- na.omit(palmerpenguins::penguins) %>%
ggplot() +
geom_point(aes(x = bill_length_mm, y = body_mass_g, color = species)) +
theme_classic()
base +
ggh4x::facet_grid2(species ~ sex, axes = "all", scales = "free_y", independent = "y") +
ggh4x::facetted_pos_scales(y = list(
sex == "female" ~ scale_y_continuous(),
sex == "male" ~ scale_y_continuous(guide = guide_axis_manual(labels = NULL, breaks = NULL))
))
Created on 2022-02-03 by the reprex package (v2.0.1)
The simplest solution in this case would be to keep the x-axes, discard the inner y-axes and draw a line instead of formally having an axis.
base +
facet_grid2(species ~ sex, axes = "x", scales = "free_y") +
geom_segment(
data = data.frame(sex = "male"),
aes(x = -Inf, xend = -Inf, y = -Inf, yend = Inf),
size = 1
)
Thanks so much for the quick reply! This suits my purpose - the geom_segment trick is also super neat!
The problem arose when trying to achieve the following layout, where the y-axis labels and breaks are NULL when
sex == "male"
My first attempt was the following with
facetted_pos_scales()
, but it seems to get ignored and produces the same plot without that line:Interestingly the conditions do get applied if I switch the order of
species ~ sex
tosex ~ species
insidefacet_grid2()
, such that thesex
variable is now in the rows instead of columns.This same asymmetry is also found for switching x-scales with
facetted_pos_scales()
as well (it can be conditioned on column values but not row values).I may be approaching the problem wrong, and any pointers would be appreciated - thanks in advance!