【问题标题】:ggplot2 gives error message inside a package but works if source the R function?ggplot2 在包内给出错误消息,但如果源 R 函数有效?
【发布时间】:2015-02-16 18:54:05
【问题描述】:

我编写了一个包含许多功能的 R 包。一切正常。但是当我运行一个使用 ggplot2 和 2 个 data.frames 的函数时,它会给出错误。但是,如果我一步一步地采购或做,它就可以工作。 有什么不同? 为什么它不能从包中工作?

例子:

install_github("mbertalan/iPsychCNV")
library(iPsychCNV)
# CNV.Res and roi are loaded from the package.
PlotAllCNVs(df=CNV.Res, Name="Test.png", roi=roi)
Error: Don't know how to add o to a plot

但是如果我在 github (PlotAllCNVs.R) 上复制代码,粘贴到 R 中并重新运行相同的命令。它可以工作,创建绘图并返回一个 data.frame。

代码:

b <- ggplot(tmp2, aes(Start, Indx))
b <- b + geom_segment(aes(x = Start, y = Indx, xend = Stop, yend = Indx, colour=as.factor(Class)))
b <- b + scale_colour_manual(values = c("ROI" = Colors[6],"q" = Colors[5],"p" = Colors[4], "1" = Colors[1], "3" = Colors[2], "4" = Colors[3], "0"=Colors[7], "2"=Colors[8]))
b <- b + facet_wrap(~ Titles, scales = "free", ncol = NCOL) 
b <- b + geom_vline(aes(xintercept = c(Start, Stop)), tmp2ROI, alpha=0.2) 

【问题讨论】:

  • 有很多事情可能是错误的。您是否将必要的功能导入到包中?至少,您提供了实际错误会有所帮助。
  • 这似乎是grid.text 的错误。确保导入了正确的函数。具有相同错误的问题是 here 但同样没有可重现的东西,我们只能猜测。
  • 这是一个开始,请将您的评论移至上面的问题,如果可以,请提供一些示例数据以重现错误。
  • 正如我所说,我只能猜测当我无法重现您的错误时。请更新您的问题并包含一些示例数据以重现错误。
  • 重现错误的唯一方法是安装包并从包中运行函数。如果您复制代码和 Rdata,它将起作用。如果您能将这些信息发送给我,我将不胜感激。

标签: r ggplot2 packages


【解决方案1】:

在 ggplot 中传递数据框似乎创建了一个层。然后它不能添加到情节(在包中)。但是,如果情节是 geom_point (我测试过),它就可以工作。

b <- ggplot(tmp2, aes(Start, Indx))
b <- b + geom_segment(aes(x = Start, y = Indx, xend = Stop, yend = Indx, colour=as.factor(Class)))
Error: Don't know how to add o to a plot

如果您在 geom_segment 中添加数据框,它会起作用:

b <- ggplot() + geom_segment(data=tmp2, aes(x = Start, y = Indx, xend = Stop, yend = Indx, colour=as.factor(Class)))

由于某种原因,在包内部,它不能覆盖图层。然后给你错误:

Error: Don't know how to add o to a plot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多