2017-10-20 84 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