Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cowplot::plot_grid()垂直对齐不同高度的绘图(当使用coord_equal()时)_R_Ggplot2_Gtable_Cowplot - Fatal编程技术网

使用cowplot::plot_grid()垂直对齐不同高度的绘图(当使用coord_equal()时)

使用cowplot::plot_grid()垂直对齐不同高度的绘图(当使用coord_equal()时),r,ggplot2,gtable,cowplot,R,Ggplot2,Gtable,Cowplot,我正在尝试使用cowplot::plot_grid()组合两个ggplot对象,并将它们垂直对齐。使用align=“v”通常非常简单 我可以通过玩弄rel_heights参数来实现我的目标,但这不是一个可行的解决方案,因为我有很多动态图要构建。这里,y轴对齐,所有轴的坐标仍然相等 cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v", rel_heights = c(2, 1.07)) 我见过许多类似问题的解决方法,它们利用了ggpl

我正在尝试使用
cowplot::plot_grid()
组合两个ggplot对象,并将它们垂直对齐。使用
align=“v”
通常非常简单

我可以通过玩弄
rel_heights
参数来实现我的目标,但这不是一个可行的解决方案,因为我有很多动态图要构建。这里,y轴对齐,所有轴的坐标仍然相等

cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v", rel_heights = c(2, 1.07))


我见过许多类似问题的解决方法,它们利用了
ggplot2::ggplotGrob()
grid::grid_draw()
,但当使用
coord_equal()
时,没有什么能解决这个问题。也许最好的解决方案根本不使用
cowplot::plot\u grid()
,或者也许解决方案是以某种方式动态确定并将正确的值传递给
rel\u heights
。我想我更喜欢后面的选项,以便能够轻松使用
cowplot::plot\u grid()
附带的其他功能。也许可以从中找到一些有用的启示。

默认情况下,轴的范围实际上超出了ggplot中的限制。函数
scale\u continuous/discrete()
中的
expand
参数用于设置扩展。如
scale\u continuous()
文档中所述:

长度为2的数值向量,给出乘法和加法展开式 常数。这些常量确保数据放置在远离轴的位置。对于连续变量,默认值为c(0.05,0),对于离散变量,默认值为c(0,0.6)

然后,使用组合这两个图

library(patchwork)
#  actual heights of plots was used to set the heights argment
plot1 + plot2 + plot_layout(ncol = 1, heights = c(y1, y2))

这里是
cowplot::plot\u grid()
的作者。当您试图将绘图与指定的纵横比对齐时,它不起作用,而使用
coord_equal()
时会生成纵横比。解决方案是使用egg库或补丁库。补丁仍在开发中,但应该很快发布给CRAN。同时,您可以从github安装

这里有一个使用鸡蛋的解决方案。在我看来,它工作得很好

library(ggplot2)
library(egg)

dat1 <- data.frame(x = rep(1:10, 2), y = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
ggarrange(plot1, plot2, ncol = 1)
库(ggplot2)
图书馆(蛋)

dat1是
egg::ggarrange(plot1,plot2)
您想要的类型?我尝试了
egg::ggarrange(plot1,plot2,ncol=1)
并且它确实进行了垂直对齐,但是y轴坐标没有保持与x轴坐标相等。y轴在顶部图中稍微拉伸。此外,我似乎再也找不到github上的
egg
——担心它可能不再被维护。这是一个遗憾,但也许该软件包中还有其他功能可以帮助我(它不在github上有点奇怪,但是)不是每一个软件都是在github上开发的。
egg
的维护程序不是在github上开发的,但是这个库现在维护得很好。@ClausWilke;有点奇怪的是,baptiste/egg是在github上开发的(从github安装它有很多答案),但它已经从github上删除了。嗯,gridExtra也被删除了,所以似乎改变了方法克劳斯,谢谢你重申egg::ggarrange()确实有效。正如你所指出的,是什么欺骗了我,使我认为轴仍然不完全正确,是因为它们的扩展方式不同@user20650,很抱歉我错过了这个,并且怀疑了你的建议!但是,当包含镶嵌面时,egg::ggarrange()解决方案会崩溃。对于如何将此推广到刻面,有什么建议吗?我已经更新了原始问题以反映这一点。凯文,请不要通过编辑问题来增加问题的复杂性。最好接受这里的一个答案,认为它对原始问题是正确的,然后针对更复杂的情况发布一个新问题(带有刻面)。已经回答的人不想追逐移动的目标,还没有看到问题的人也不太可能看到编辑。好了,明白了。新的问题是:
library(ggplot2)
dat1 <- data.frame(x = rep(1:10, 2), y = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
# The defaults are c(0.05, 0) for your continuous variables
limity1 <- max(dat1$y) - min(dat1$y)
y1 <- limity1 + 2 * limity1 * 0.05
limity2 <- max(dat2$y) - min(dat2$y)
y2 <- limity2 + 2 * limity2 * 0.05
library(patchwork)
#  actual heights of plots was used to set the heights argment
plot1 + plot2 + plot_layout(ncol = 1, heights = c(y1, y2))
library(ggplot2)
library(egg)

dat1 <- data.frame(x = rep(1:10, 2), y = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
ggarrange(plot1, plot2, ncol = 1)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + 
  scale_y_continuous(limits = c(0, 21), breaks = 5*(0:4), expand = c(0, 0)) +
  coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + 
  scale_y_continuous(limits = c(0, 11), breaks = 5*(0:4), expand = c(0, 0)) +
  coord_equal()
ggarrange(plot1, plot2, ncol = 1)