ggplot "error in rename"
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 开发者_Go百科me what object is causing this error or why - how can I figure this out?
Does anyone know what may have gone wrong?
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))
The problem is caused because ggplot2 doesn't use namespaces - this will be fixed in the next release.
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.
精彩评论