我正在尝试使用coord_equal()
或cowplot::plot_grid()
将两个FACETED ggplot对象与egg::ggarrange()
组合在一起,并垂直对齐它们。egg::ggarrange()
方法适用于UNFACETED地块,解决方案发布在here上。
但是,当包含构面时,egg::ggarrange()
解决方案将失效。 这些图已正确对齐,但是y轴的单位是x轴的单位的两倍。关于如何将其概括为面的任何建议?
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)
最佳答案
这似乎是一个简单的解决方法,
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')
关于r - 使用coord_equal()时将不同高度的多面ggplots垂直对齐,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48961927/