Store output from gridExtra::grid.arrange into an object
The code in your edit does not work properly since you didn't load gridExtra
.
library(gridExtra)
y <- arrangeGrob(p1, p2, ncol = 1)
class(y)
#[1] "gtable" "grob" "gDesc"
grid.draw(y)
Edit: since version 2.0.0, my comment about grid
dependency below is no longer valid, since grid
is now imported.
Edit: With gridExtra version >= 2.0.0, there is no need to attach either package,
p <- ggplot2::qplot(1,1)
x <- gridExtra::arrangeGrob(p, p)
grid::grid.draw(x)
Saving grid.arrange() plot to file
grid.arrange
draws directly on a device. arrangeGrob
, on the other hand, doesn't draw anything but returns a grob g
, that you can pass to ggsave(file="whatever.pdf", g)
.
The reason it works differently than with ggplot objects, where by default the last plot is being saved if not specified, is that ggplot2 invisibly keeps track of the latest plot, and I don't think grid.arrange
should mess with this counter private to the package.
store arrangeGrob to object, does not create printable object
The gridExtra
package has been updated recently thereby changing how arrangeGrob
works internally and what kind of object it returns (now a gtable
).
You need to call grid.draw
:
grid.draw(y)
Edit: do not use plot()
as initially suggested; it will add a grey background, and is only meant to be used for debugging gtables.
Assign grid.arrange to object
If you look at the source code for grid.arrange
, it is simply a wrapper for arrangeGrob
function (..., as.table = FALSE, clip = TRUE, main = NULL, sub = NULL,
left = NULL, legend = NULL, newpage = TRUE)
{
if (newpage)
grid.newpage()
grid.draw(arrangeGrob(..., as.table = as.table, clip = clip,
main = main, sub = sub, left = left, legend = legend))
}
Therefore
require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip()
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip()
gA <- ggplotGrob(A)
gB <- ggplotGrob(B)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
x <- arrangeGrob(gA, gB, ncol=1)
y <- arrangeGrob(gA, gB, ncol=1)
grid.arrange(x, y, ncol=2)
will work
How to create a list of ggplot objects that grid.arrange will accept and draw in r
You can try to :
- Initialise the length of the list because growing objects in a loop is considerably slow.
- Use
.data
pronoun to subset the names so you get proper names on x-axis.
library(ggplot2)
library(gridExtra)
test_fun = function (x) {
plt_lst = vector('list', length(x))
nm <- names(x)
for(i in seq_along(x)){
plt_lst[[i]] = ggplot(data = x, aes(x = .data[[nm[i]]])) + geom_histogram()
}
return(plt_lst)
}
test_plt_lst = test_fun(df)
do.call(grid.arrange, test_plt_lst)
grid.arrange (Gridextra) Error when combining survival curves stored in a list
The issue is that the object returned by ggsurvplot
isn't a ggplot object. It's an object of class ggsurvplot
which is simply a list. The plot itself is just one element of this list with name plot
.
Hence, to solve your issue you have to first extract the plots from your list_kp_plot_month
list using e.g. lapply(list_kp_plot_month,
[[, "plot")
Making use of a simplified example using the lung
dataset:
library(survival)
library(survminer)
library(gridExtra)
# Create example data. Just 2 months
list_data_month <- list(month1 = lung, month2 = lung)
list_kp_month <- list()
list_kp_plot_month <- list()
for (i in seq_along(list_data_month)) {
# Kaplan Meier estimates
list_kp_month[[i]] <- survfit(Surv(time = time, event = status) ~ sex, data = list_data_month[[i]], robust = TRUE)
# Kaplan Meier curves
list_kp_plot_month[[i]] <- ggsurvplot(
fit = list_kp_month[[i]],
palette = NULL, # Couleur, e.g. "blue"
linetype = 1, # Ligne "solide"
surv.median.line = "none", # Essayer "hv"
conf.int = TRUE,
risk.table = FALSE, # Peut être "TRUE"
cumevents = FALSE,
cumcensor = FALSE,
tables.height = 0.25,
xlab = "Months",
legend = "bottom",
legend.title = "",
legend.labs = c("Control", "Treatment")
)
}
# Gridextra - Combining the 12 Kaplan Meier curves together
n <- length(list_kp_plot_month)
nCol <- floor(sqrt(n))
# Get plots from ggsurvplot objects
plots <- lapply(list_kp_plot_month, `[[`, "plot")
do.call("grid.arrange", c(plots, ncol = nCol))
Related Topics
Change Font Size for All Inline Equations R Markdown
How to Give Numbers to Each Group of a Dataframe with Dplyr::Group_By
All Paths in Directed Tree Graph from Root to Leaves in Igraph R
Obtain Date Column from Xts Object
Plot The Intensity of a Continuous with Geom_Tile in Ggplot
Inserting Logo into Beamer Presentation Using R Markdown
Classification Functions in Linear Discriminant Analysis in R
Data.Table Join (Multiple) Selected Columns with New Names
Plot Weighted Frequency Matrix
Using Recordlinkage to Add a Column with a Number for Each Person
Conda Build R Package Fails at C Compiler Issue on Macos Mojave
Why Ggplot2 Legend Not Show in The Graph
Include Link to Local HTML File in Datatable in Shiny