R 以相同的打印宽度但不同的打印高度排列多个GGP打印
我尝试使用gridExtra包安排两个不同绘图高度但相同绘图宽度的GGPlot 我有一个相同宽度或相同高度的解决方案,但我无法同时实现这两个目标 这是我的密码:R 以相同的打印宽度但不同的打印高度排列多个GGP打印,r,ggplot2,gridextra,R,Ggplot2,Gridextra,我尝试使用gridExtra包安排两个不同绘图高度但相同绘图宽度的GGPlot 我有一个相同宽度或相同高度的解决方案,但我无法同时实现这两个目标 这是我的密码: require(ggplot2) require(gridExtra) set.seed(987) dat <- data.frame(x = 1:100, y1 = rnorm(100), y2 = rnorm(100)*1e6) p1 <- ggplot(da
require(ggplot2)
require(gridExtra)
set.seed(987)
dat <- data.frame(x = 1:100,
y1 = rnorm(100),
y2 = rnorm(100)*1e6)
p1 <- ggplot(dat, aes(x = x, y = y1)) +
geom_point() + ylab("")
p2 <- ggplot(dat, aes(x = x, y = y2)) +
geom_point() + ylab("")
# Arrange Plots
# Version 1
# same widths, same heights
grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), size = "last"))
# Version 2
# different heights, different widths
grid.arrange(p1, p2, ncol = 1, heights = c(2, 1))
require(ggplot2)
需要(额外)
种子(987)
dat多亏了@Gregor和他的链接,我可以使用另一个问题的大部分代码,只是做了一些小改动。这就是它最终的样子:
gb1 <- ggplot_build(p1)
gb2 <- ggplot_build(p2)
# work out how many y breaks for each plot
n1 <- length(gb1$panel$ranges[[1]]$y.labels)
n2 <- length(gb2$panel$ranges[[1]]$y.labels)
gA <- ggplot_gtable(gb1)
gB <- ggplot_gtable(gb2)
# combine both plots (last should really be "pmax", it's an unfortunate bug)
g <- gtable:::rbind_gtable(gA, gB, "last")
# locate the panels in the gtable layout
panels <- g$layout$t[grepl("panel", g$layout$name)]
# assign new (relative) heights to the panels, based on the number of breaks
g$heights[panels] <- list(unit(n1*2,"null"), unit(n2, "null"))
# notice the *2 here to get different heights
grid.newpage()
grid.draw(g)
gb1的意思是尽可能地重复这一点:我试图在三个图上实现这一点,但这不起作用?应该是吗?我只用了两次,从来没有试过更多。它似乎不适用于两次以上