12

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!

tonytonov
  • 25,060
  • 16
  • 82
  • 98
user3831246
  • 121
  • 1
  • 1
  • 3
  • Check out `?geom_area`, it requires `y` aesthetic. Also check out [this](http://stackoverflow.com/questions/4651428/making-a-stacked-area-plot-using-ggplot2) question. – tonytonov Jul 14 '14 at 09:25
  • Thanks @tonytonov -- I tried geom_area previously and got the same error. What do you mean it "requires y aesthetic"? is that a package of some sort? – user3831246 Jul 14 '14 at 20:43
  • 1
    Since you've recently started learning ggplot, the best recommendation is to read Hadley Wickham's book. It is also a good idea to start using `ggplot` calls instead of `qplot`, it'll make your life easier in the long run. – tonytonov Jul 15 '14 at 07:45

2 Answers2

8

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.

Community
  • 1
  • 1
tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • @ tonytonov, thanks VERY much. The resulting plot is viable, but it's not quite what I had in mind. I was hoping to produce solid-colored shapes, with the dispersal shadow of the less important disperses overlaid on top of the solid shapes of the major dispersers for comparison. Why can't I just define a ymax and then use the geom_area function? How would I do that? I'm totally new to ggplot (and stackoverflow) so I apologize if I'm making a basic error. – user3831246 Jul 15 '14 at 18:21
  • You are welcome. Can you show an example of the plot you're trying to produce? You can, of course, map an aesthetic to `ymax`, but what will it be? [Here](http://www.cookbook-r.com/Graphs/) you may find an example of your plot. – tonytonov Jul 16 '14 at 05:51
  • 1
    Hello again @tonytonov-- I solved the problem by putting the distances into bins of 10 meters and making the no. of seeds in each bin the y variable; then the geom_area() function worked just fine. Thanks for helping me figure it out! – user3831246 Jul 17 '14 at 14:27
  • 3
    You are welcome! I encourage you to write a short answer to your own question, so that any future readers will see the solution. – tonytonov Jul 17 '14 at 19:44
8

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.

bluenote10
  • 23,414
  • 14
  • 122
  • 178