Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep all plot components same size in ggplot2 between two plots

Tags:

r

ggplot2

I would like two separate plots. I am using them in different frames of a beamer presentation and I will add one line to the other (eventually, not in example below). Thus I do not want the presentation to "skip" ("jump" ?) from one slide to the next slide. I would like it to look like the line is being added naturally. The below code I believe shows the problem. It is subtle, but not how the plot area of the second plot is slightly larger than of the first plot. This happens because of the y axis label.

library(ggplot2)

dfr1 <- data.frame(
  time = 1:10,
  value = runif(10)  
)

dfr2 <- data.frame(
  time = 1:10,
  value = runif(10, 1000, 1001)  
)

p1 <- ggplot(dfr1, aes(time, value)) + geom_line() + scale_y_continuous(breaks = NULL) + scale_x_continuous(breaks = NULL) + ylab(expression(hat(z)==hat(gamma)[1]*time+hat(gamma)[4]*time^2))
print(p1)
dev.new()
p2 <- ggplot(dfr2, aes(time, value)) + geom_line() + scale_y_continuous(breaks = NULL) + scale_x_continuous(breaks = NULL) + ylab(".")
print(p2)

I would prefer to not have a hackish solution such as setting the size of the axis label manually or adding spaces on the x-axis (see one reference below), because I will use this technique in several settings and the labels can change at any time (I like reproducibility so want a flexible solution).

I'm searched a lot and have found the following:

Specifying ggplot2 panel width

How can I make consistent-width plots in ggplot (with legends)?

https://groups.google.com/forum/#!topic/ggplot2/2MNoYtX8EEY

How can I add variable size y-axis labels in R with ggplot2 without changing the plot width?

They do not work for me, mainly because I need separate plots, so it is not a matter of aligning them virtically on one combined plot as in some of the above solutions.

like image 205
Xu Wang Avatar asked Jul 12 '14 03:07

Xu Wang


People also ask

How do I combine two GG plots?

Combine the plots over multiple pagesThe function ggarrange() [ggpubr] provides a convenient solution to arrange multiple ggplots over multiple pages. After specifying the arguments nrow and ncol, ggarrange()` computes automatically the number of pages required to hold the list of the plots.

How do I arrange multiple Ggplots?

To arrange multiple ggplots on one single page, we'll use the function ggarrange()[in ggpubr], which is a wrapper around the function plot_grid() [in cowplot package]. Compared to the standard function plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.

How do I put two ggplot together?

Combine multiple ggplot on one page.Use the function ggarrange() [ggpubr package], a wrapper around the function plot_grid() [cowplot package]. Compared to plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.


2 Answers

haven't tried, but this might work,

gl <- lapply(list(p1,p2), ggplotGrob)
library(grid)
widths <- do.call(unit.pmax, lapply(gl, "[[", "widths"))
heights <- do.call(unit.pmax, lapply(gl, "[[", "heights"))
lg <- lapply(gl, function(g) {g$widths <- widths; g$heights <- heights; g})
grid.newpage()
grid.draw(lg[[1]])
grid.newpage()
grid.draw(lg[[2]])
like image 198
baptiste Avatar answered Oct 17 '22 10:10

baptiste


How about using this for p2:

p2 <- ggplot(dfr2, aes(time, value)) + geom_line() + 
  scale_y_continuous(breaks = NULL) + 
  scale_x_continuous(breaks = NULL) + 
  ylab(expression(hat(z)==hat(gamma)[1]*time+hat(gamma)[4]*time^2)) +
  theme(axis.title.y=element_text(color=NA))

This has the same label as p1, but the color is NA so it doesn't display. You could also use color="white".

like image 33
jlhoward Avatar answered Oct 17 '22 08:10

jlhoward