3

In ggplot I want to create a facet grid where the y axes are free. This scales option is available in facet_wrap() but I want to keep the grid layout. I have attached a drawn example of what I want.

example of facet grid with free y axes

EDIT: I added some code for calrity

# Create data
nX <- 3
nY <- 3
nVal <- 2
df <- data.frame(M = sort(rep(paste0("M",1:nX), nVal * nY)),
                 n = rep(sort(paste0("n",rep((1:nY)-1, nVal))),nX),
                 G = rep(1:2,nX * nY),
                 val = c(100,300,20,10,1,2,10,25,NA,NA,0.1,0.2,25,27,2,5,0.5,0.6))

# Delete observations to resemble my data
df <- subset(df, !is.na(val))

# scales does not work in facet_grid(), thus obscuring trends for low values
ggplot(df, aes(x = G,
               y = val)) +
  geom_line()+
  ylim(0,NA)+
  facet_grid(n ~ M,scales = "free_y")

with facet grid

Note that no trends are visible for low values because in the grid they are obscured by the high values.

# the grid is lost in facet_wrap()
ggplot(df, aes(x = G,
               y = val)) +
  geom_line()+
  ylim(0,NA)+
  facet_wrap(n+M~.,scales = "free_y")

enter image description here

Note that the grid layout is lost.

moooh
  • 459
  • 3
  • 10

2 Answers2

8

The dev/github version of ggh4x has facet_grid2() which allows these independent axes within a grid layout. (Disclaimer: I'm the author of ggh4x.) If you find any bugs or unclear documentation, feel free to leave an issue on the github since I don't think this has been field-tested a lot.

library(ggplot2)
library(ggh4x) # devtools::install_github("teunbrand/ggh4x")

# Create data
nX <- 3
nY <- 3
nVal <- 2
df <- data.frame(M = sort(rep(paste0("M",1:nX), nVal * nY)),
                 n = rep(sort(paste0("n",rep((1:nY)-1, nVal))),nX),
                 G = rep(1:2,nX * nY),
                 val = c(100,300,20,10,1,2,10,25,NA,NA,0.1,0.2,25,27,2,5,0.5,0.6))

# Delete observations to resemble my data
df <- subset(df, !is.na(val))

# scales does not work in facet_grid(), thus obscuring trends for low values
ggplot(df, aes(x = G,
               y = val)) +
  geom_line()+
  ylim(0,NA)+
  facet_grid2(n ~ M,scales = "free_y", independent = "y")

Created on 2021-06-09 by the reprex package (v1.0.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • This look great! I'm gonna try it out and let you know. – moooh Jun 09 '21 at 20:29
  • This really fills a gap in ggplot, thank you! – Antón Jul 07 '22 at 10:39
  • Hello, I have a problem with the package. I now want the y-axis has the same scale within each column and different between columns. How am I able to achieve this? i.e., i want scales to fixed in y but I want independent y – wut Jan 27 '23 at 00:47
  • That sounds like two contradictory goals to me. Could you explain a bit more? – teunbrand Jan 27 '23 at 08:08
0

Unfortunately this isn’t possible by default - see this post for more info. Probably some approach using {patchwork} is your best bet, but there are also other other approaches you can take if you’re willing to look under the hood of {ggplot2}, for example the one outlined here

wurli
  • 2,314
  • 10
  • 17