Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ggsave with a pipe

I can save a plot with ggsave after I stored it, but using it in a pipeline I get the following error. I wish to plot and save in the same (piped) command.

  no applicable method for 'grid.draw' applied to an object of class "c('LayerInstance', 'Layer', 'ggproto', 'gg')" 

I know ggsave's arguments are first the filename, and then the plot, but switching this in a wrapper does not work. Also, using 'filename=' and 'plot=' in the ggsave command does not work.

library(magrittr)
library(ggplot2)
data("diamonds")

# my custom save function
customSave <- function(plot){
    ggsave('blaa.bmp', plot)
}

#This works:
p2 <- ggplot(diamonds, aes(x=cut)) + geom_bar()
p2 %>% customSave()

# This doesn't work:
ggplot(diamonds, aes(x=cut)) + geom_bar() %>% customSave()

# and obviously this doesn't work either
ggplot(diamonds, aes(x=cut)) + geom_bar() %>% ggsave('plot.bmp')
like image 670
fliestech Avatar asked Feb 12 '19 10:02

fliestech


People also ask

What does ggsave() do?

When TRUE (the default), ggsave () will not save images larger than 50x50 inches, to prevent the common error of specifying dimensions in pixels. Background colour. If NULL, uses the plot.background fill value from the plot theme. ... Other arguments passed on to the graphics device function, as specified by device.

How to add a ggsave to a ggplot addition pipe?

You can no longer "add" ggsave to a ggplot addition-pipe. Edit: this is a recent change in ggplot2-3.3.4. The previous answer is preserved below if you want to work around the new behavior.

How to save the ggsave in R programming?

The syntax to save the ggsave in R Programming is. ggsave(filename) and the complex syntax behind this R ggsave is: ggsave(filename, plot = last_plot(), device = NULL, path = NULL, scale = 1, width = NA, height = NA, dpi = 300, limitsize = TRUE, .., units = c("in", "cm", "mm")) Create R ggplot Scatter plot

Does ggsave save images larger than 50x50?

When TRUE (the default), ggsave () will not save images larger than 50x50 inches, to prevent the common error of specifying dimensions in pixels. Background colour. If NULL, uses the plot.background fill value from the plot theme. ...


2 Answers

Update: The following code no longer works. See the tidyverse GitHub issue and another StackOverflow question for the official solution.

Original answer that NO LONGER WORKS:

If you want to plot and save in one line, try this

ggsave('plot.bmp') ```

If you don't want to show the plot, simply put `p <- ` at the
beginning. 

If you have a custom save function, you can also do this

``` mysave <- function(filename) {   ggsave(file.path("plots",
paste0(filename, ".png")), 
         width = 8, height = 6, dpi = 300) } ```

and simply replace `ggsave('plot.bmp')` with `mysave('plot')` in the
snippet above.

I found this usage by accident but haven't found any documentation.
like image 129
Jiāgěng Avatar answered Oct 20 '22 05:10

Jiāgěng


As akrun pointed out, you need to wrap all of your ggplot in parentheses. You can also use the dot notation to pass an object to a function parameter other than the first in a magrittr pipe stream:

library(magrittr)
library(ggplot2)
data("diamonds")

(
  ggplot(diamonds, aes(x=cut)) +
    geom_bar()
) %>% 
  ggsave("plot.png", . , dpi = 100, width = 4, height = 4)
like image 42
DanTan Avatar answered Oct 20 '22 05:10

DanTan