r - 在 `facet_wrap` ed网格中,将子图居中于0,同时保持 `free_x`

标签 r ggplot2

在下面的图中,我有一个多面网格。

有什么办法可以将两个子图居于0的中心,同时为min轴保留不同的max / x值?

在下面的情况下,左为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")

enter image description here

最佳答案

我还需要这样的东西才能并排显示不对称光谱,

enter image description here

试试这个功能,

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

关于r - 在 `facet_wrap` ed网格中,将子图居中于0,同时保持 `free_x`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635016/

相关文章:

r - 使用 dplyr mutate 根据列名向量创建新列

r - 为 PDF 输出添加 natbib 选项

r - 如何使用另一个测试结果中的标签来标记 ggplot 中的条形图?

r - 在带有刻面的图形上添加 R^2

r - 如何在ggplot中添加表达式到标签

R:计算列中文本数据的长度

r - 如何在 R 中仅显示带标签的数据框中的一种类型的属性

r - 在 r 中绘制板布局热图

r - 错误条在堆积条形图中的位置不正确 (r)

R ggplot2-帮助复制火车图