Vertical alignment of graphs of different heights using cowplot :: plot_grid () when using the coord_equal () function

I am trying to combine two ggplot objects with cowplot::plot_grid()and vertically align them. This is usually pretty simple using align = "v".

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()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point()
cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v")

image 1

However, this approach does not work when ggplots are used coord_equal()because it plot_grid()cannot change the axes when formatting. Instead, the default value is to keep the height of each graph.

plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v")

image 2

I can get my goal to play with and get the argument rel_heightsin order, but this is not a viable solution since I have a lot of dynamic builds. Here, the y axes are aligned, and the coordinates of all the axes are still equal.

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

image 3

, ggplot2::ggplotGrob() grid::grid_draw(), coord_equal() . , cowplot::plot_grid(), , , - rel_heights. , , , cowplot::plot_grid(). , - .

+2
2

cowplot::plot_grid() . , , coord_equal(). , . , 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)

enter image description here

, , , (1) y , , , (2) . .

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)

enter image description here

+1

ggplot. expand scale_continuous/discrete() . scale_continuous():

, . , . c (0,05, 0) c (0, 0,6) .

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()

, , , expand.

# 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

patchwork

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

enter image description here

+1

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


All Articles