adelriosantiago
adelriosantiago

Reputation: 8124

Equal height for 2 plots in ggarrange

For simplicity here is a runnable code with fake data that produces the issue exactly. The plots,

library(ggplot2)
library(ggpubr)

data1 <- data.frame(
  V1=replicate(18, {sample(1:18, 1)}),
  V2=replicate(18, {sample(1:3, 1)}),
  V3=replicate(18, {sample(1:3, 1)})
)

graph1 <- ggplot(data1, aes(x=V1, y=V2, fill=V3))+geom_tile(size=0.5)+coord_equal()

data2 <- data.frame(
  V1=replicate(18, {sample(1:3, 1)}),
  V2=replicate(18, {sample(1:3, 1)})
)
graph2 <- ggplot(data2, aes(x=c("A"), y=V1,fill=V2))+geom_tile(size=0.5)+coord_equal()

ggarrange(graph1, graph2, labels = c("A", "B"), ncol = 2, nrow = 1)

generate,

enter image description here

and as you can see the first plot shrinks because its too large. How do I prevent the first graph from shrinking even if that will produce a very large in width image?

Note that the result is similar to what is produced when running ggarrange with nrow=5 but if I do that then the graph is flat and all other elements look weird:

enter image description here

Using widths as suggested in one answer generates:

enter image description here

Which have different heights. I assume it may look just fine on some screens settings. How can I ensure that both plots always have the same height no matter the viewport size?

Upvotes: 1

Views: 3786

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26630

One option is to specify the relative widths of each figure, e.g.

library(tidyverse)
library(ggpubr)
data1 <- data.frame(
  V1=replicate(18, {sample(1:18, 1)}),
  V2=replicate(18, {sample(1:3, 1)}),
  V3=replicate(18, {sample(1:3, 1)})
)

graph1 <- ggplot(data1, aes(x=V1, y=V2, fill=V3))+geom_tile(size=0.5)+coord_equal()

data2 <- data.frame(
  V1=replicate(18, {sample(1:3, 1)}),
  V2=replicate(18, {sample(1:3, 1)})
)
graph2 <- ggplot(data2, aes(x=c("A"), y=V1,fill=V2))+geom_tile(size=0.5)+coord_equal()

ggarrange(graph1, graph2, labels = c("A", "B"), ncol = 2, nrow = 1, widths = c(0.85, 0.15))

You can specify the dimensions when you save the plot:

ggsave(filename = "example.png", width = 12, height = 3, units = "in")

example2.png

EDIT In an RMarkdown file, you can also specify fig.height and fig.width to alter the dimensions of the final figure, e.g.


> ---
> title: "test"
> author: "test"
> date: "11/12/2020"
> output: html_document
> ---
> 
> ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ```
> 
> ```{r plot, fig.height=3, fig.width=12}
> library(tidyverse)
> library(ggpubr)
> data1 <- data.frame(   V1=replicate(18, {sample(1:18,
> 1)}),   V2=replicate(18, {sample(1:3, 1)}),   V3=replicate(18,
> {sample(1:3, 1)}) )
> 
> graph1 <- ggplot(data1, aes(x=V1, y=V2,
> fill=V3))+geom_tile(size=0.5)+coord_equal()
> 
> data2 <- data.frame(   V1=replicate(18, {sample(1:3, 1)}),  
> V2=replicate(18, {sample(1:3, 1)}) ) graph2 <- ggplot(data2,
> aes(x=c("A"), y=V1,fill=V2))+geom_tile(size=0.5)+coord_equal()
> 
> ggarrange(graph1, graph2, labels = c("A", "B"), ncol = 2, nrow = 1,
> widths = c(0.85, 0.15))
> ```

rmarkdown_example.png

Upvotes: 2

Related Questions