2017-10-20 85 views
0

考虑这些虚拟数据ggplot2 - facet_grid:我如何能够沿着Y轴有不同范围的值,但零点是否对齐?

set.seed(12) 
y = c(rnorm(120,0,1), rnorm(120,3,1), rnorm(120,0,120)) 
x = y + rnorm(120*3, 0,1) 
d = data.frame(y=y,x=x,group=rep(1:3,each=120)) 

我想显示使用ggplot2每个groupxy之间的关系。我也想在y = 0处添加一条水平线。

我试图

ggplot(d, aes(x=x,y=y)) + geom_point() + facet_wrap(~group, nrow=1, scales="free_x") + geom_hline(yintercept = 0) 

enter image description here

但我们没有前两个pannels看到太多的差异在y。所以,我想

ggplot(d, aes(x=x,y=y)) + geom_point() + facet_wrap(~group, scales="free", nrow=1) + geom_hline(yintercept = 0) 

enter image description here

但现在的水平线是不是在这困扰我相同的水平。

我怎样才能有不同的值范围沿着Y轴,但与零(或任何其他数字)完全对齐?

回答

1
egg::symmetrise_scale(last_plot(), 'y') 

enter image description here

+0

大,一个简单的办法! – Prradep

1

则可以使用下面的代码实现:

ggplot(d, aes(x=x, y=y)) + 
    geom_point() + 
    geom_blank(aes(y=-y)) + 
    facet_wrap(~group, scale="free", nrow=1) + 
    geom_hline(yintercept = 0) 

enter image description here