Stan Shunpike
Stan Shunpike

Reputation: 2234

Preserving text size with `grid.arrange`

Problem

I have 4 graphs that I want to display using grid.arrange(). When I display them individually, they look like this:

enter image description here

But when I use grid.arrange(), they become distorted

enter image description here

with them individually looking like

enter image description here

Specific Issues:

  1. The x-axis labels do not scale and overlap, making them unreadable.
  2. The subtitles get cutoff.

Goal

I want to reproduce each plot exactly like the first ideal case in a grid with grid.arrange(). One possible way might be to convert each plot to an image and then use grid.arrange() but I don't know how to do this.

Reproducible Example

Below is an example reproducible code that shows the problem I am having.

p1 <- ggplot(subset(mtcars, cyl = 4), aes(wt, mpg, colour = cyl)) + geom_point() + labs(title = "TITLE-TITLE-TITLE-TITLE-TITLE-TITLE", subtitle = "-subtitle-subtitle-subtitle-subtitle-subtitle-subtitle-subtitle-") +theme(plot.title = element_text(hjust = 0.5),plot.subtitle = element_text(hjust = 0.5)) 
p2 <- ggplot(subset(mtcars, cyl = 4), aes(wt, mpg, colour = cyl)) + geom_point() + labs(title = "TITLE-TITLE-TITLE-TITLE-TITLE-TITLE", subtitle = "-subtitle-subtitle-subtitle-subtitle-subtitle-subtitle-subtitle-") +theme(plot.title = element_text(hjust = 0.5),plot.subtitle = element_text(hjust = 0.5)) 

grid.arrange(p1, p2, ncol = 2)

Upvotes: 3

Views: 2955

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

When you display those graphs individually they simply have more space. So, those are natural distortions and there are perhaps only three ways to solve that.

  1. When exporting the combined graph, make it big enough. If the individual one looks good in 6x5 inches, then surely the combined one will look good in 12x10 inches.

  2. Give correspondingly less space for the problematic parts: x-axis labels and the subtitle. For instance, use something like element_text(size = 6) for plot.subtitle and axis.title.x, add \n to the subtitles and even x-axis labels, try something like element_text(angle = 30) for the latter as well.

  3. Get rid of something unnecessary. As @Richard Telford suggests in the comments, using facet_wrap should work better. That would be due to, e.g., not repeating the y-axis labels and, hence, giving more horizontal space.

Upvotes: 2

Related Questions