Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggsave(): Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "character"

Tags:

r

ggplot2

I am trying to save a plot with ggsave(). I enter the following:

library(ggplot2)

Test = data.frame("X" = seq(1, 10, 1), "Y" = 2*seq(1, 10, 1))

P = ggplot(
    Test, aes(x=X, y=Y))+
    geom_line()

ggsave(P, "test.pdf", device = "pdf")

But get the error:

Saving 7 x 7 in image
Error in UseMethod("grid.draw") : 
  no applicable method for 'grid.draw' applied to an object of class "character"
like image 689
Michael Ohlrogge Avatar asked Apr 02 '17 16:04

Michael Ohlrogge


People also ask

How do I use Ggsave?

In most cases ggsave() is the simplest way to save your plot, but sometimes you may wish to save the plot by writing directly to a graphics device. To do this, you can open a regular R graphics device such as png() or pdf() , print the plot, and then close the device using dev. off() .

What package has Ggsave?

The ggsave() is a method in the ggplot2 package which is used for saving the last plot that was displayed in the screen.

What are the default dimensions that Ggsave () saves an image as?

2.1. The default size of the saved image is equal to the size of Plots pane (the “graphics device”) in RStudio, which can be found with dev. size() . Notice that the result of dev. size() and the message we receive when saving the plot with ggsave() give the same dimensions.

Where do Ggsave files go?

ggsave. ggsave works by default on the most recent graph that you've plotted. The only arugment it needs is a file name to save it as. By default, it will save to the directory that you're working out of.


1 Answers

Many R functions that save data, such as write.table(), saveRDS() etc. take as their first argument the object to be saved. But, this is not true for ggsave(). Instead, by default, its first argument is the name of the file to save to. Thus, the syntax above would need to be modified in one of two ways:

ggsave(plot = P, filename = "test.pdf", device = "pdf")
ggsave("test.pdf", P, device = "pdf")
like image 168
Michael Ohlrogge Avatar answered Sep 28 '22 23:09

Michael Ohlrogge