Creating arbitrary panes in ggplot2
Following Josh O'Brien's example: I'm surprised no one has mentioned grid.arrange
from the gridExtra
package yet:
library(gridExtra)
grid.arrange(q1,q2,q3,q4,q5,q6,nrow=3)
This seems to be mentioned here: multiple graphs in one canvas using ggplot2
For me, it's much easier than remembering all the viewport stuff.
Creating arbitrary panes in ggplot2
Following Josh O'Brien's example: I'm surprised no one has mentioned grid.arrange
from the gridExtra
package yet:
library(gridExtra)
grid.arrange(q1,q2,q3,q4,q5,q6,nrow=3)
This seems to be mentioned here: multiple graphs in one canvas using ggplot2
For me, it's much easier than remembering all the viewport stuff.
Layout for multiple ggplot2 plots when some are centered
One way to arrange multiple plots is with multipanelfigure
package.
Create example plots:
library(ggplot2)
plots <- list()
for(i in 1:5) {
plots[[i]] <- ggplot() + ggtitle(paste("My plot:", i))
}
Create panel for plots:
library(multipanelfigure)
figure <- multi_panel_figure(columns = 4, rows = 3, panel_label_type = "none")
We use function multi_panel_figure()
with specified number of columns and rows. You needed only 2 columns, but as you want to center one plot we will use 4 columns (center will be columns 2 & 3).
Add plots on panel:
figure %<>%
fill_panel(plots[[1]], column = 2:3, row = 1) %<>%
fill_panel(plots[[2]], column = 1:2, row = 2) %<>%
fill_panel(plots[[3]], column = 3:4, row = 2) %<>%
fill_panel(plots[[4]], column = 1:2, row = 3) %<>%
fill_panel(plots[[5]], column = 3:4, row = 3)
Result:
Adding the same component to a list of ggplots
User H 1 answered the question. As ggplot2 is different due to the layering, I was unsure if lapply()
would work in this case. I now learned that the pipe symbol, +
is a function to be applied over.
But adding a title and positioning the legend at the bottom has the desired effect:
q <- lapply(l, function(x) x + ggtitle("Cars") + theme(legend.position = "bottom"))
multiplot( plotlist = q, cols = 2)
where the code for multiplot()
is found here.
show multiple plots from ggplot on one page in r
You can save all the plot in a list
then use either cowplot::plot_grid()
or gridExtra::marrangeGrob()
to put them in one or more pages
See also:
Creating arbitrary panes in ggplot2 (
patchwork
,multipanelfigure
&egg
packages)Multiple plots in for loop
library(tidyverse)
# create a list with a specific length
plot_lst <- vector("list", length = 8)
for (i in 1:8) {
g <- ggplot(data = mtcars, aes(x = hp, y = wt)) +
geom_point()
plot_lst[[i]] <- g
}
# Combine all plots
cowplot::plot_grid(plotlist = plot_lst, nrow = 4)
library(gridExtra)
ml1 <- marrangeGrob(plot_lst, nrow = 2, ncol = 2)
ml1
Created on 2018-09-20 by the reprex package (v0.2.1.9000)
How can I obtain an 'unbalanced' grid of ggplots?
grid.arrange
draws directly on the device; if you want to combine it with other grid objects you need arrangeGrob
, as in
p = rectGrob()
grid.arrange(p, arrangeGrob(p,p,p, heights=c(3/4, 1/4, 1/4), ncol=1),
ncol=2)
Edit (07/2015): with v>2.0.0 you can use the layout_matrix
argument,
grid.arrange(p,p,p,p, layout_matrix = cbind(c(1,1,1), c(2,3,4)))
Showing multiple axis labels using ggplot2 with facet_wrap in R
You can do this by including the scales="free" option in your facet_wrap call:
myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replace=T)
myValues <- rnorm(300)
df <- data.frame(myGroups, myValues)
p <- ggplot(df) +
geom_density(aes(myValues), fill = alpha("#335785", .6)) +
facet_wrap(~ myGroups, scales="free")
p
multiple graphs in one canvas using ggplot2
You can use grid.arrange()
in the gridExtra package like this:
grid.arrange(p1, p2)
Related Topics
Long/Bigint/Decimal Equivalent Datatype in R
What Is the Purpose of Setting a Key in Data.Table
Read Multiple CSV Files into Separate Data Frames
Using Data.Table Package Inside My Own Package
Select Groups With More Than One Distinct Value
Starting Shiny App After Password Input
How to Format a Number as Percentage in R
Pasting Two Vectors With Combinations of All Vectors' Elements
Calculate Cumulative Sum (Cumsum) by Group
Getting Warning: " 'Newdata' Had 1 Row But Variables Found Have 32 Rows" on Predict.Lm
Assign Multiple Columns Using := in Data.Table, by Group
Capitalize the First Letter of Both Words in a Two Word String
What Are the "Standard Unambiguous Date" Formats For String-To-Date Conversion in R
How to Assign a Unique Id Number to Each Group of Identical Values in a Column
Using Regex in R to Find Strings as Whole Words (But Not Strings as Part of Words)
Generate a Sequence of the Last Day of the Month Over Two Years