2015-05-29 84 views
3

是否有可能用gridExtra(或其他包)覆蓋兩個地塊?ggplot:覆蓋兩個地塊

我想重新調整一個情節和它覆蓋到的第二個(指定縮放和座標)

require(ggplot2) 
require(gridExtra) 

df <- data.frame(value=rnorm(10), date=1:10) 

p1 <- ggplot(data.frame(df), aes(value,date)) + geom_line() 
p2 <- ggplot(data.frame(df), aes(value,date)) + geom_point() 

gtable包組合獲得這樣的事情

enter image description here

+0

'P1; print(p2,vp = viewport(.8,.75,.4,0.4))' – user20650

+0

[嵌入圖中的微縮圖]可能重複(http://stackoverflow.com/questions/7793935/embedding-a -miniature-plot-in-a-plot) – user20650

回答

2

看與gridExtra。您可以根據需要指定圖的大小和座標。

require(gtable) 

p1 <- ggplotGrob(p1) 
p2 <- ggplotGrob(p2) 

gt <- gtable(widths = unit(c(1, 2), "null"), heights = unit(c(.2, 1, 1), "null")) 
gt <- gtable_add_grob(gt, p2, t = 1, b = 3, l = 1, r = 2) 
gt <- gtable_add_grob(gt, p1, t = 2, l = 2) 
grid.draw(gt) 

enter image description here

+0

你需要加載'grid'包才能夠使用'grid.draw' –