本文介绍了ggsave():UseMethod("grid.draw")中的错误:没有将适用于"grid.draw"的适用方法应用于类"character"的对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggsave()保存地块.我输入以下内容:

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")

但是得到错误:

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函数(例如write.table()saveRDS()等)将要保存的对象作为其第一个参数.但是,对于ggsave(),这不是.相反,默认情况下,其第一个参数是要保存到的文件的名称.因此,需要以以下两种方式之一修改上面的语法:

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")

这篇关于ggsave():UseMethod("grid.draw")中的错误:没有将适用于"grid.draw"的适用方法应用于类"character"的对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 22:28