在`facet_wrap`ed网格中,中心子图为0,同时保持"free_x"

Mic*_*ert 20 r ggplot2

在下面的图中,我有一个刻面网格.

有没有办法将两个子图集中在一起0,同时保持轴的不同min/ maxx

在下面的情况下,这将是xlim=c(-1,1)左和xlim=c(-2,2)右,但它应该是普遍适用的.

(在现实生活中的例子中,那些是刻面的火山地块,我想以0效果大小居中,但保持x不同地块的不同尺度)

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

bap*_*ste 16

我也需要这样的东西来并排显示不对称光谱,

在此输入图像描述

试试这个功能,

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

symmetrise_scale(p, "y")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • [支持多个分面变量的暂定版本](https://gist.github.com/baptiste/2cafb70b46428a3b298c6a5078c38284) (3认同)