1

I cannot seem to save a plot when using ggdraw() + draw_grob(). Saving just 'plot1' alone works fine but once I start using ggdraw it no longer works with either ggsave() or save_plot(), it produces an error noting an invalid canvas value. Example below:

library(ggplot2)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'
library(cowplot)
library(grid)


plot1 <-  ggplot(mpg, aes(x = hwy, y = cty)) +
          geom_point()

line <-  linesGrob(x = unit(c(0,1), "npc"), y = unit(c(0,1), "npc"))

plot_full <- ggdraw(plot1) +
              draw_grob(line)

ggsave("plotfull.png", plot_full)
#> Saving 7 x 5 in image
#> Error in grDevices::png(..., res = dpi, units = "in"): invalid value of 'canvas'


save_plot("plotfull.png", plot_full)
#> Error in grDevices::png(..., res = dpi, units = "in"): invalid value of 'canvas'

Created on 2020-09-14 by the reprex package (v0.3.0)

Stephan
  • 2,056
  • 1
  • 9
  • 20
andrew
  • 11
  • 2
  • 1
    I cannot reproduce your error (both `ggsave` and `save_plot` run without issue). R version 4.0.2, cowplot_1.1.0, ggplot2_3.3.2 – Greg Sep 14 '20 at 17:32
  • Thanks for attempting I am also using R 4.02, cowplot 1.1.0, and ggplot2 3.3.2. – andrew Sep 14 '20 at 18:16
  • A couple more questions: What OS are you on? Have you tried to open a png device, print the plot, and then close? Have you tried to set a specific png device with `type = ...`. Have you tried to use the `agg_png()` device from the ragg package? – Claus Wilke Sep 14 '20 at 21:15
  • For saving a plot without using `ggsave()`, follow the process described here: https://cran.r-project.org/web/packages/ragg/readme/README.html – Claus Wilke Sep 14 '20 at 21:17
  • Thanks, I am using windows. The same error occurs when setting the device with `device=` or `type =` within `ggsave()`. It works and saves when not using `ggsave()` as per the link you provided. – andrew Sep 15 '20 at 06:20

0 Answers0