I am aware that facet_grid
does not allow freeing scaling within rows or columns. So I am trying my luck with facet_wrap
instead.
In addition to what I have so far,
data <- data.frame(
cols = c("A", "A", "A", "A", "B", "B", "B", "B"),
rows = c(1, 1, 2, 2, 1, 1, 2, 2),
x = c(0, 90, 0, 110, 0, 900, 0, 1100),
y = c(0, 90, 0, 110, 0, 900, 0, 1100)
)
library(ggplot2)
p <- ggplot(data, aes(x = x, y = y)) +
geom_line() +
facet_wrap(~ rows + cols, scales = "free")
how can I fix the horizontal scales in the columns (read: each column separately)? In other words, how to align the horizontal [0, 75] axis with the horizontal [0, 90] axis, and the horizontal [0, 750] axis with the horizontal [0, 900] axis?
Ideally, this would hide the horizontal axis labels in all but the last row.
If you dare venture into github packages, I've recently added an extended version of facet_grid()
to the dev version of ggh4x package (disclaimer: I wrote the package). It discriminates scales being 'free' -that can vary between rows / columns- from scales that are 'independent' and free, which can also vary within rows / columns. I guess the functions fits your problem pretty well.
library(ggplot2)
library(ggh4x) # devtools::install_github("teunbrand/ggh4x")
data <- data.frame(
cols = c("A", "A", "A", "A", "B", "B", "B", "B"),
rows = c(1, 1, 2, 2, 1, 1, 2, 2),
x = c(0, 90, 0, 110, 0, 900, 0, 1100),
y = c(0, 90, 0, 110, 0, 900, 0, 1100)
)
library(ggplot2)
ggplot(data, aes(x = x, y = y)) +
geom_line() +
facet_grid2(rows ~ cols, scales = "free", independent = "y")
Created on 2021-04-16 by the reprex package (v1.0.0)
Since I only added the function last week or so and it hasn't been tested extensively, please let me know if you find any bugs or have suggestions for improvements.