ggsave(): Error in UseMethod(“grid.draw”) : no app

2019-04-29 18:07发布

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"

标签: r ggplot2
1条回答
该账号已被封号
2楼-- · 2019-04-29 18:34

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, "test.pdf", device = "pdf")
ggsave("test.pdf", P, device = "pdf")
查看更多
登录 后发表回答