使用coord_equal()时将不同高度的多面ggplots垂直对齐 [英] Vertically align faceted ggplots of different heights when using coord_equal()

查看:115
本文介绍了使用coord_equal()时将不同高度的多面ggplots垂直对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cowplot::plot_grid()egg::ggarrange()将两个FACETED ggplot对象与coord_equal()组合在一起,并垂直对齐它们.

I am trying to combine two FACETED ggplot objects with coord_equal() using cowplot::plot_grid() or egg::ggarrange() and vertically align them.

egg::ggarrange()方法适用于UNFACETED绘图,解决方案

The egg::ggarrange() approach works fine for UNFACETED plots, with the solution posted here.

但是,当包含构面时,egg::ggarrange()解决方案将失效. 图已正确对齐,但y轴的单位是x轴的单位的两倍.关于如何对此进行概括的任何建议?

However, the egg::ggarrange() solution breaks down when faceting is included. The plots are correctly aligned, but the units of the y-axes are twice as large as those of the x-axes. Any suggestions for how to generalize this for faceting?

dat1 <- data.frame(x = rep(1:10, 2), y = 1:20, z = rep(c("A", "B"), 10))
dat2 <- data.frame(x = 1:10, y = 1:10, z = rep(c("A", "B"), 5))
plot1 <- ggplot(dat1, aes(x=x, y=y)) + 
  geom_point() + coord_equal() + facet_wrap(~z)
plot2 <- ggplot(dat2, aes(x=x, y=y)) + 
  geom_point() + coord_equal() + facet_wrap(~z)
egg::ggarrange(plot1, plot2, ncol = 1)

推荐答案

这似乎是一个简单的解决方法,

it seems to be a simple fix,

library(egg)

b <- body(gtable_frame)
b[6] <- parse(text="if (fixed_ar) {
    ar <- as.numeric(g$heights[tt[1]]) / as.numeric(g$widths[ll[1]])
    height <- width * (ar / length(ll))
    g$respect <- FALSE
}")

body(gtable_frame) <- b

assignInNamespace("gtable_frame", gtable_frame, ns = 'egg')

这篇关于使用coord_equal()时将不同高度的多面ggplots垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆