In the plot below, I have a faceted grid.
Is there any way to center both subplots at 0
, while keeping different min
/max
values for the x
axis?
In the case below that would be xlim=c(-1,1)
for left and xlim=c(-2,2)
for right, but it should be generally applicable.
(in a real life example, those are faceted volcano plots and I want to center at 0
effect size but keep the different x
scales for different plots)
library(ggplot2)
df = data.frame(x=c(1,2), y=c(0,0), group=c(1,2))
ggplot(df, aes(x=x, y=y)) + geom_point() + facet_wrap(~group, scale="free_x")
The facet_wrap () function can be used to produce multi-panel plots in ggplot2. library(ggplot2) ggplot (df, aes(x_var, y_var)) + geom_point () + facet_wrap (vars (category_var)) The following examples show how to use this function with the built-in mpg dataset in R:
Source: R/facet-grid-.r facet_grid () forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap ().
This plot shows the ribbon layout for subplots (just one plot after another, filling the first row and then moving on to the next) sorted by alignment then gender. Note that with 8 panels ggplot2 opted for three rows and three columns.
How to Use facet_wrap in R (With Examples) The facet_wrap () function can be used to produce multi-panel plots in ggplot2. This function uses the following basic syntax: library(ggplot2) ggplot (df, aes(x_var, y_var)) + geom_point () + facet_wrap (vars (category_var))
I've also needed something like this to display asymmetric spectra side-by-side,
Try this function,
symmetrise_scale <- function(p, axis = "x"){
gb <- ggplot_build(p)
type <- switch(axis, "x" = "x.range", "y" = "y.range")
lims <- sapply(gb$panel$ranges, "[[", type)
fname <- as.character(p$facet$facets)
facets <- gb$panel$layout[[fname]]
lims2 <- as.vector(t(tcrossprod(apply(abs(lims), 2, max), c(-1,1))))
dummy <- setNames(data.frame(rep(facets, each=2), lims2), c(fname, axis))
switch(axis,
"x" = p + geom_blank(data=dummy, aes(x=x, y=Inf), inherit.aes = FALSE),
"y" = p + geom_blank(data=dummy, aes(x=Inf, y=y), inherit.aes = FALSE))
}
library(ggplot2)
df = data.frame(x=c(1,2), y=c(5,0.2), group=c(1,2))
p <- ggplot(df, aes(x=x, y=y)) + geom_point() + facet_wrap(~group, scale="free")
symmetrise_scale(p, "x")
symmetrise_scale(p, "y")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With