Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a grid plot in R?

Tags:

r

ggplot2

I have a grid plot object g.

class(g)
"gtable" "grob"   "gDesc" 

I can use grid.draw(g) to draw the plot. However, I cannot figure out a way to save the plot to a pdf file.

I tried:

ggsave(g, file="plot.png")

But apparently ggsave doesn't work on such an object.

Here is an example from the ?grid.draw help page:

grid.newpage()
## Create a graphical object, but don't draw it
l <- linesGrob()
## Draw it
grid.draw(l)

Drawing works well, but saving/printing causes the problem.

Any workaround on this? Thanks!

like image 226
xiaoxiao87 Avatar asked Apr 17 '15 20:04

xiaoxiao87


People also ask

How do you save a grid plot?

You can use arrangeGrob function that returns a grob g that you can pass to the ggsave function to save the plot. Whereas grid. arrange draws directly on a device and by default, the last plot is saved if not specified i.e., the ggplot2 invisibly keeps track of the latest plot.

How do you save a plot in R?

Plots panel –> Export –> Save as Image or Save as PDF It's also possible to save the graph using R codes as follow: Specify files to save your image using a function such as jpeg(), png(), svg() or pdf(). Additional argument indicating the width and the height of the image can be also used. Create the plot.

How do you make a grid plot in R?

Creating a Grid of Plots To do this, you use the parameter value mfrow=c(x,y) where x is the number of rows that you wish to have in your plot and y is the number of columns. When you plot, R will place each plot, in order by row within the grid that you define using mfrow .

How do I save a figure as a pdf in R?

Once you've created a plot in R, you may wish to save it to a file so you can use it in another document. To do this, you'll use either the pdf() , png() or jpeg() functions. These functions will save your plot to either a . pdf, .


2 Answers

This is what MrFlick answered, but for PDFs (what you asked for in your question).

## Initiate writing to PDF file
pdf("path/to/file/PDFofG.pdf", height = 11, width = 8.5, paper = "letter")

## Create a graphical object g here
g # print it

## Stop writing to the PDF file
dev.off()
like image 146
Paul James Avatar answered Sep 21 '22 12:09

Paul James


It may be worth adding that updated ggsave version facilites the desired export.

Packages

# Load
lapply(c("ggplot2",
         "gridExtra"), 
       require, 
       character.only = TRUE)
sessionInfo()

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gridExtra_2.2.1 ggplot2_2.1.0  

loaded via a namespace (and not attached):
[1] colorspace_1.2-6 grid_3.1.1       gtable_0.2.0     munsell_0.4.3    plyr_1.8.3       Rcpp_0.12.6     
[7] scales_0.4.0     tools_3.1.1  

Graph preparation and export

a  <- ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = cyl))

b  <- ggplot(data = mtcars) +
  geom_line(aes(x = wt, y = vs))

# grid
gridAB  <- grid.arrange(a, b)
# Export
ggsave(filename="ab.pdf", plot=gridAB)

Class

> class(gridAB)
[1] "gtable" "gTree"  "grob"   "gDesc" 

Preview

Results

like image 30
Konrad Avatar answered Sep 21 '22 12:09

Konrad