在`facet_wrap`ed网格中,在保留`free_x`的同时以0为中心绘制子图 [英] In a `facet_wrap`ed grid, center subplots at 0 while keeping `free_x`
问题描述
有什么办法可以将两个子图放在 0
,同时为 x $ c $保留不同的
min
/ max
值c> axis?
在下面的情况下,左边是 xlim = c(-1,1)
和 xlim = c(-2,2)
表示正确,但它应该是通用的。
(in一个真实的例子,那些是多面的火山地块,我想集中在 0
效果大小,但保持不同
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)
我有一个也需要像这样并排显示不对称光谱,
试试这个函数,
symmetrise_scale < - function(p,axis =x){
gb< - ggplot_build(p)
type< - 开关(轴,x=x.range,y=y.range)
lims< - sapply(gb $ panel $ ranges,[[,type)
fname< - as.character(p $ facet $ facets)
facets< - gb $ panel $ layout [[fname (b)(b)(b)(b)(b)(b)(b)(b) 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))
}
库(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)
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")
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")
这篇关于在`facet_wrap`ed网格中,在保留`free_x`的同时以0为中心绘制子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!