13

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.

Community
  • 1
  • 1
Xu Wang
  • 10,199
  • 6
  • 44
  • 78

2 Answers2

12

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]])
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • 1
    This does indeed work. I am so curious. Why did you not try yourself? I imagine that time is 5 seconds to paste into R but maybe you had no access to R. You were able to do all of that from memory without testing? In any case, thank you for your creative solution. – Xu Wang Jul 15 '14 at 02:48
2

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".

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • This is very good idea. Thank you. However I need a y label on p2. In my example I have `ylab(".")`. – Xu Wang Jul 15 '14 at 02:31
  • I have tried to adapt the responses at here https://groups.google.com/forum/#!topic/ggplot2/s5Euy1orwLY but have failed to adapt. – Xu Wang Jul 15 '14 at 02:47