Vertically align faceted ggplots of different heights when using the cop_equal () function

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

The approach is egg::ggarrange()great for UNFACETED charts, and the solution is published here .

However, the solution egg::ggarrange()breaks when the cut is turned on. The graphs are correctly aligned, but the units of the y-axis are two times larger than the x-axis . Any suggestions on how to generalize this for a cut?

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)

image 1

+4
source share
2 answers

this seems like 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')

enter image description here

+2
source

, plot1 plot2 . plot1: enter image description here

plot2:

enter image description here

, .. theme(aspect.ratio=1) coord_equal():

require(ggplot2)
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() + theme(aspect.ratio=1)+
  facet_wrap(~z)
plot2 <- ggplot(dat2, aes(x=x, y=y)) + geom_point() + theme(aspect.ratio=1)+
  facet_wrap(~z)
egg::ggarrange(plot1, plot2, ncol = 1,heights = c(1,10))

enter image description here

, .

+2

Source: https://habr.com/ru/post/1694080/


All Articles