gtable
Multiple ggplots of different sizes
You can use nested arrangeGrob calls like this example: library(ggplot2) library(gridExtra) p <- ggplot(data.frame(x=1, y=1), aes(x,y)) + geom_point() grid.arrange( arrangeGrob( p, arrangeGrob(p, p, nrow=2), ncol=2 ,widths=c(2,1)), arrangeGrob(p, p ,p ,ncol=3, widths=rep(1,3)), nrow=2) Edit: gl <- lapply(1:9, function(ii) grobTree(rectGrob(),textGrob(ii))) grid.arrange( arrangeGrob(gl[[1]], do.call(arrangeGrob, c(gl[2:5], ncol=2)), nrow=1, widths=3:2), do.call(arrangeGrob, c(gl[6:9], nrow=1, list(widths=c(1,1,1,2)))), nrow=2, heights=c(2,1))
ggplot2 – adding secondary y-axis on top of a plot
Updated to ggplot2 v 2.2.1, but it is easier to use sec.axis – see here Original From ggplot2 version 2.1.0, the business of moving axes around became a lot more complex, the reason being that the labels became complex grobs containing text grobs and margins. (There is also a bug with axis.line. A temporary workaround … Read more
how to show a legend on dual y-axis ggplot
Similar to the technique you use above you can extract the legends, bind them and then overwrite the plot legend with them. So starting from # draw it in your code # extract legend leg1 <- g1$grobs[[which(g1$layout$name == “guide-box”)]] leg2 <- g2$grobs[[which(g2$layout$name == “guide-box”)]] g$grobs[[which(g$layout$name == “guide-box”)]] <- gtable:::cbind_gtable(leg1, leg2, “first”) grid.draw(g)
Seeking workaround for gtable_add_grob code broken by ggplot 2.2.0
Indeed, ggplot2 v2.2.0 constructs complex strips column by column, with each column a single grob. This can be checked by extracting one strip, then examining its structure. Using your plot: library(ggplot2) library(gtable) library(grid) # Your data df = structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, … Read more
Save plot with a given aspect ratio
You can use grid functions to calculate the full size of the ggplot grob, but there are (edit: at least) two caveats: an extra device window will open, to do the unit conversion the plot panel size will be 0 by default, as it is meant to be calculated on-the-fly according to the device (viewport) … Read more
Align ggplot2 plots vertically
Here’s an example to align more basic grobs, library(ggplot2) library(grid) library(gtable) p <- qplot(1,1) g <- ggplotGrob(p) panel_id <- g$layout[g$layout$name == “panel”,c(“t”,”l”)] g <- gtable_add_cols(g, unit(1,”cm”)) g <- gtable_add_grob(g, rectGrob(gp=gpar(fill=”red”)), t = panel_id$t, l = ncol(g)) g <- gtable_add_rows(g, unit(1,”in”), 0) g <- gtable_add_grob(g, rectGrob(gp=gpar(fill=”blue”)), t = 1, l = panel_id$l) grid.newpage() grid.draw(g) and with … Read more
How to use facets with a dual y-axis ggplot
Now that ggplot2 has secondary axis support this has become much much easier in many (but not all) cases. No grob manipulation needed. Even though it is supposed to only allow for simple linear transformations of the same data, such as different measurement scales, we can manually rescale one of the variables first to at … Read more