Search code examples
rggplot2density-plot

argument "env" is missing, with no default qplot or ggplot R


I have a dataset with 3 columns for which I'm trying to plot the pdf for column ID. Here's what part of my data looks like.

        day          ID   count
        8754     48112050     1
        8975     48112050     3
        8327     61010046     2
        8346     61010046  3997
        8506     61010046     1
        8605     61010046     1

I use qplot for this :

qplot(count, colour=factor(ID), data=df, geom="density")

or ggplot:

ggplot(df, aes(x=count, colour= ID))+geom_density()

but it does not plot the pdf for all IDs. When I dig in, I realize that only IDs that have no more than 2 occurrences in my data are missing in the plot produced by qplot or ggplot. In this example, ID:48112050.

I plot the density for this ID only, and it works.

         day          ID   count
        8754     48112050     1
        8975     48112050     3

However, when I limit my df to just include this ID, or any ID with just 2 occurrences, the qplot or ggplot gives me the following error:

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

Does this mean that qplot/ggplot needs at least 3 points to plot the density function?


Solution

  • It looks like ggplot2_1.0.1 does require at least three points to estimate a density. However, this behavior appears to have changed in the github repository with this commit on Jun 12,2015. There version that's currently on CRAN was published in March. I'm not sure when to expect the next release of ggplot2. You could try pulling the source directly from github if this behavior is essential to you,