Plot facet_wrap with free scales but with same limits

Hi all!

Is there some way to plot a graph in ggplot2 where I use facet_wrap with scales = "free", but where I set the limits to be the same for the x- and the y-axis in each of the faceted plots? I want to do scatterplots where I have percentages at the axes. The comparison will be easier if the two axes have the same breaks and limits. If I don't set scales = "free", I can achieve this, but then axes are not adapted to the points in each of the faceted plots.

I could, of course, make separate plots and combine them using patchwork, but it would be easier if I could use facet_wrap in some way.

Any help would be greatly appreciated!
R

I don't follow. If you want the breaks and the limits to be the same in each panel, then what about the axes should be adapted to the data?

Maybe you could make each plot by different plot and next make a union with grid.arrange() from library(gridExtra)

And remember put a reproducible example for better help you all the community.

Sorry if I wasn't clear. See the example below. I would like the axes in each plot to have the same limits and breaks so that the dashed line will be 45 degrees. Preferably, I would also like to be able to set the axes to start at zero.

library(tidyverse)
tibble(x = runif(50, 0, .03),
            y = runif(50, 0, .07),
            group = "A") |> 
  bind_rows(tibble(x = runif(50, 0.4, .9),
                   y = runif(50, 0.5, .7),
                   group = "B")) |> 
  ggplot(aes(x, y)) +
  geom_point() +
  facet_wrap(~group, scales = "free") +
  geom_abline(slope = 1, color = "red", linetype = "dashed")

Created on 2022-09-12 with reprex v2.0.2

Best, Richard

Ahh I see. That's not (easily) possible with only ggplot2, but you can use the ggh4x package:

E.g. add this to your plot

your_plot +
  ggh4x::facetted_pos_scales(
    x = list(
      scale_x_continuous(limits = c(0, .1)),
      scale_x_continuous(limits = c(0, 1))
    ),
    y = list(
      scale_y_continuous(limits = c(0, .1)),
      scale_y_continuous(limits = c(0, 1))
    )
  )
1 Like

Great, thank you so much! I didn't know about ggh4x. Much appreciated! /R

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.