Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: error in qplot from ggplot2: argument "env" is missing, with no default

Tags:

r

ggplot2

I am using qplot from ggplot2 to plot the distances of seeds dispersed by different species in R. When I use geom='density', it works just fine! But what I really want is a frequency/area plot, for which I get an error I do not know how to address.

This works:

qplot(Dist,data=testx,geom="density",fill=Animal,log=c('x','y'),alpha=I(0.5))

This doesn't work:

qplot(Dist,data=testx,geom="area",fill=Animal,log=c('x','y'))

Error in exists(name, envir = env, mode = mode) : 
  argument "env" is missing, with no default

Help? Thanks!

like image 235
user3831246 Avatar asked Jul 11 '14 23:07

user3831246


2 Answers

The reason for that error (the message is quite obscure, I agree) is that you are trying to use geom_area (qplot(geom = "area") is roughly the same as + geom_area()). Whereas geom_density only requires x (x = Dist in your case), this is not enough for geom_area, as it additionally uses ymax (for help pages, see this, which links to this).

Here's an example of density and frequency plots which you may adjust for your data:

ggplot(data=diamonds, aes(x=carat, fill=clarity)) + geom_density(alpha=0.5)
ggplot(data=diamonds, aes(x=carat, colour=clarity)) + geom_freqpoly()

Your code sample is not reproducible, so I cannot verify the following line, but

ggplot(data=testx, aes(x=Dist, colour=Animal)) + geom_freqpoly() + 
  scale_x_log10() + scale_y_log10()

may be what you need.

like image 112
tonytonov Avatar answered Oct 19 '22 18:10

tonytonov


Regarding this error message it might help to point out that this is the error message you get when you use an empty data set for a histogram:

df <- data.frame(testx = rnorm(0))
p <- ggplot(df, aes(x=testx)) +
  geom_histogram()
plot(p)

Error in exists(name, envir = env, mode = mode) : 
  argument "env" is missing, with no default

Unfortunately, the error message is not very helpful at all in this case. When I first ran into this problem it took me some time to figure out that I just accidentally had ended up with an empty data frame. The OP probably had a different problem, but it is always good to know that this error is connected to this stupid mistake.

like image 28
bluenote10 Avatar answered Oct 19 '22 18:10

bluenote10