Reputation: 67
I have a 6x6 grid (sites along the side and year along the top), showing abundance (y) over time(x).
I have used facet_grid
to build the plot, using free_y
. This has plotted a figure with each row having a shared y axis. I need to have a free_y
for all the panels individually and I know that facet_grid
doesnt allow this, while facet_wrap
does. I would like to use facet_grid
becuase it keeps the order of the panels, with columns for each year, and rows for each site.
I've tried using the lemon
package with the facet_rep_grid
function but this hasn't worked. Does anyone know how I can free_y
across all the panels seperately?
Thanks
Upvotes: 3
Views: 1083
Reputation: 78
facet_wrap does this, and is what I use, although it does mean you have the faceting value as a title for each individual facet, which can be a pain.
ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) +
geom_point() +
facet_wrap(year ~ drv,
scales = "free_y")
Upvotes: 4
Reputation: 2292
Another solution would be facet_rep_grid
in the lemon
package.
Sample code:
#install.packages("lemon")
library(lemon)
library(ggplot2)
ggplot(df)+
geom_line(aes(x=time, y=abundance, color=sites))+
lemon::facet_rep_grid(year~sites,
scales="free",
repeat.tick.labels = "all")+
theme(panel.background = element_blank(),
axis.line = element_line(size=0.5))+
labs(x="Time", y="Abundance", fill="Sites")+
theme_minimal()
Plot:
Upvotes: 1
Reputation: 38043
One suggestion is to use ggh4x::facet_grid2()
to set independent y scales. (Disclaimer: I'm the author of ggh4x). Example with standard dataset below:
library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) +
geom_point() +
ggh4x::facet_grid2(vars(year), vars(drv),
scales = "free_y", independent = "y")
Created on 2022-03-11 by the reprex package (v0.3.0)
Upvotes: 2