ggplot“重命名错误”

发布于 2024-10-12 18:09:57 字数 940 浏览 4 评论 0原文

更新我已经在下面发布了我的解决方案,罪魁祸首是我自己的rename函数,它覆盖了reshape::rename


我一直在使用ggplot R包直到今天都没有遇到什么麻烦。今天,我使用以前有效的代码时遇到错误,当我将其调试到最小工作示例时,它仍然给出错误;

如果我这样做:

library(ggplot2)
d<- data.frame(x=1:3,y=1:3)
ggplot(data=d) + geom_line(aes(x,y))   

返回以下错误:

Error in rename(x, .base_to_ggplot) : 
  unused argument(s) (.base_to_ggplot)

回溯是:

6: rename(x, .base_to_ggplot)
5: rename_aes(aes)
4: aes()
3: structure(list(data = data, layers = list(), scales = Scales$new(), 
       mapping = mapping, options = list(), coordinates = CoordCartesian$new(), 
       facet = FacetGrid$new(), plot_env = environment), class = "ggplot")
2: ggplot.data.frame(data = d, aes = c(x, y))
1: ggplot(data = d, aes = c(x, y))

使用 rm(list=ls()) 删除所有对象后不会发生错误,但我仍然不清楚是什么对象导致了这个错误或者为什么 - 我怎样才能解决这个问题?

有谁知道可能出了什么问题?

update I have posted my solution below, the culprit was my own rename function that overrode reshape::rename


I have been using the ggplot R package with little trouble until today. Today, I get an error using code that has previously worked, and when I debug it to the minimal working example, it still gives an error;

If I do this:

library(ggplot2)
d<- data.frame(x=1:3,y=1:3)
ggplot(data=d) + geom_line(aes(x,y))   

The following error is returned:

Error in rename(x, .base_to_ggplot) : 
  unused argument(s) (.base_to_ggplot)

The traceback is:

6: rename(x, .base_to_ggplot)
5: rename_aes(aes)
4: aes()
3: structure(list(data = data, layers = list(), scales = Scales$new(), 
       mapping = mapping, options = list(), coordinates = CoordCartesian$new(), 
       facet = FacetGrid$new(), plot_env = environment), class = "ggplot")
2: ggplot.data.frame(data = d, aes = c(x, y))
1: ggplot(data = d, aes = c(x, y))

The error does not occur after removing all objects using rm(list=ls()), but it is still not clear to me what object is causing this error or why - how can I figure this out?

Does anyone know what may have gone wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾城如夢未必闌珊 2024-10-19 18:09:57

我无法返回您上面发布的相同错误消息。运行代码片段时,我收到以下错误:

Error: geom_pointrange requires the following missing aesthetics: ymin, ymax

因此,geom_pointrange() 需要 yminymax 的参数。我将让您填写有关这些参数应包含哪些内容的相关信息,但此代码执行:

ggplot(data=d) + geom_pointrange(aes(x,y, ymin = y - 。 5、ymax = y + .5))

I'm not able to return the same error message that you've posted above. When running your code snippet, I'm getting the following error:

Error: geom_pointrange requires the following missing aesthetics: ymin, ymax

Accordingly, geom_pointrange() is expecting arguments for ymin and ymax. I'll leave it up to you to fill in your pertinent information for what should go into those parameters, but this code executes:

ggplot(data=d) + geom_pointrange(aes(x,y, ymin = y - .5, ymax = y + .5))

叹倦 2024-10-19 18:09:57

该问题是因为 ggplot2 不使用命名空间而引起的 - 这将在下一个版本中修复。

The problem is caused because ggplot2 doesn't use namespaces - this will be fixed in the next release.

简美 2024-10-19 18:09:57

该错误是由其中一个对象引起的(感谢@Chase 的指针)。

这是我调试并找到罪魁祸首的方法。重要的部分是使用 try() 函数,尽管出现错误,但仍能保持 for 循环运行。

foo <- ls() #get a static list of all suspect objects
for(i in 1:length(foo)) {
    print(foo[i])
    rm(list=foo[i])
    try(ggplot()+geom_point(aes(x=1:2,y=1:2)))
}

这导致了以下输出:

...
[1] "45 reg.model"
Error in rename(x, .base_to_ggplot) : 
  unused argument(s) (.base_to_ggplot)
[1] "46 reg.parms"
Error in rename(x, .base_to_ggplot) : 
  unused argument(s) (.base_to_ggplot)
[1] "47 rename"
[1] "48 samples"
...

啊哈!这是我自己的函数 rename 导致了错误,因为 ggplot2 依赖于 reshape::rename

解决方案:重命名新的rename函数...将来如何防止这种情况发生?也许研究一下命名空间的使用。

The error was caused by one of the objects (thanks to pointers from @Chase).

Here is how I debugged and found the culprit. The important part was to use the try() function that keeps the for loop running despite errors

foo <- ls() #get a static list of all suspect objects
for(i in 1:length(foo)) {
    print(foo[i])
    rm(list=foo[i])
    try(ggplot()+geom_point(aes(x=1:2,y=1:2)))
}

This resulted in the following output:

...
[1] "45 reg.model"
Error in rename(x, .base_to_ggplot) : 
  unused argument(s) (.base_to_ggplot)
[1] "46 reg.parms"
Error in rename(x, .base_to_ggplot) : 
  unused argument(s) (.base_to_ggplot)
[1] "47 rename"
[1] "48 samples"
...

aha! it was my own function rename that caused the error, since ggplot2 relies on reshape::rename.

Solution: rename the new rename function... how to prevent this in the future? Perhaps study up on the use of namespaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文