I would like to make a plot using facet_wrap
where the axes can vary for each panel but within a panel the x
and y
axes should be the same scale.
e.g. see the following plots
df <- read.table(text = "
x y g
1 5 a
2 6 a
3 7 a
4 8 a
5 9 b
6 10 b
7 11 b
8 12 b", header = TRUE)
library(ggplot2)
ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g) # all axes 1-12
ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g, scales = "free")
# fee axes, y & y axes don't match per panel
What i want is for panel a the x and why axes both to be 1-8 and for panel b the x and y axes both to range from 5 - 12.
Is this possible?
Using this answer you could try the following:
Another solution is trick the axes for individual
facet_wrap()
plots by adding invisible points to the plots with x and y reversed so that the plotted data is "square", e.g.,You could also use
geom_blank()
. You don't need dummy data.This wasn't an option when the question was asked, but these days I would highly recommend patchwork for combining plots.