Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a `facet_wrap`ed grid, center subplots at 0 while keeping `free_x`

Tags:

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")

enter image description here

like image 748
Michael Schubert Avatar asked Jun 04 '16 21:06

Michael Schubert


People also ask

How to use the facet_wrap() function in ggplot2?

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:

What is a facet grid 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 ().

What is the ribbon layout for subplots in ggplot?

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?

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))


1 Answers

I've also needed something like this to display asymmetric spectra side-by-side,

enter image description here

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")

enter image description here

symmetrise_scale(p, "y")

enter image description here

like image 92
baptiste Avatar answered Sep 27 '22 19:09

baptiste