![]() There is also a vignette explaining how to make plots with a shared legend. (The ncol = 2 argument tells save_plot() that there are two plots side-by-side, and save_plot() makes the saved image twice as wide.)įor a more in-depth description of how to arrange plots in a grid see this vignette. The object that plot_grid() returns is another ggplot2 object, and you can save it with ggsave() as usual: p <- plot_grid(iris1, iris2, labels = "AUTO")Īlternatively, you can use the cowplot function save_plot(), which is a thin wrapper around ggsave() that makes it easy to get the correct dimensions for combined plots, e.g.: p <- plot_grid(iris1, iris2, labels = "AUTO") Iris2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + Iris1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + I wrote the cowplot package to solve this (and a few other) issues, specifically the function plot_grid(): library(cowplot) One downside of the solutions based on grid.arrange is that they make it difficult to label the plots with letters (A, B, etc.), as most journals require. See the answer by below and this vignette for an equivalent approach but the function allows finer controls on plot location and size, based on this vignette. The plot_grid function in the cowplot is worth checking out as an alternative to grid.arrange. This is pointed out below in many answers below, but I want to highlight this approach with examples equivalent to the above plots. This not only saves time arranging data, it is necessary when you want two dissimilar plots.įacets are helpful for making similar plots for different groups. This is the equivalent of making two distinct plots using par(mfrow = c(1,2)). Or, use arrangeGrob() in combination with ggsave(), ggsave("foo.pdf", arrangeGrob(plot1, plot2)) To print the side effect to a file, specify a device driver (such as pdf, png, etc), e.g. This will plot the output as a side effect. This is useful when the two plots are not based on the same data, for example if you want to plot different variables without using reshape(). ![]() The function grid.arrange() in the gridExtra package will combine multiple plots this is how you put two side by side. Any ggplots side-by-side (or n plots on a grid)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |