Error that seems misleading when using geom_line(stat="density") without enough data
Closed this issue · 2 comments
lcolladotor commented
Hello,
In a regular EDA of a specific type of results, I found an error using ggplot() + geom_line(stat="density"). At first I was confused by the error because the plot was working for other data sets. Tracing things, I realized that the real problem was because there was not enough data to calculate the density.
I'll change my EDA analysis so this doesn't happen again (attempting to plot without enough data), but it'd be nice if the ggplot2 error was more informative.
Here's a small reproducible example:
> library(ggplot2)
> df <- data.frame(width = c(2806, 320, 287, 132, 214, 202, 235, 266, 156, 210),
seqnames=factor(c("chr20", "chr1", "chr6", "chr16", "chr17", "chr4", "chr1", "chr19", "chrX", "chr17")))
> ggplot(df, aes(x=log10(width), colour=seqnames)) + geom_line(stat="density")
Error in exists(name, env) : argument "env" is missing, with no default
> traceback()
6: exists(name, env)
5: find_global(scale_name, env)
4: scales_add_missing(plot, c("x", "y"))
3: ggplot_build(x)
2: print.ggplot(list(data = list(width = c(2806, 320, 287, 132,
214, 202, 235, 266, 156, 210), seqnames = c(5L, 1L, 7L, 2L, 3L,
6L, 1L, 4L, 8L, 3L)), layers = list(<environment>), scales = <S4 object of class "Scales">,
mapping = list(x = log10(width), colour = seqnames), theme = list(),
coordinates = list(limits = list(x = NULL, y = NULL)), facet = list(
shrink = TRUE), plot_env = <environment>, labels = list(
x = "log10(width)", colour = "seqnames", y = "density",
fill = "NA")))
1: print(list(data = list(width = c(2806, 320, 287, 132, 214, 202,
235, 266, 156, 210), seqnames = c(5L, 1L, 7L, 2L, 3L, 6L, 1L,
4L, 8L, 3L)), layers = list(<environment>), scales = <S4 object of class "Scales">,
mapping = list(x = log10(width), colour = seqnames), theme = list(),
coordinates = list(limits = list(x = NULL, y = NULL)), facet = list(
shrink = TRUE), plot_env = <environment>, labels = list(
x = "log10(width)", colour = "seqnames", y = "density",
fill = "NA")))
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 digest_0.6.4 grid_3.1.0 gtable_0.1.2
[5] MASS_7.3-32 munsell_0.4.2 plyr_1.8.1 proto_0.3-10
[9] Rcpp_0.11.1 reshape2_1.4 scales_0.2.4 stringr_0.6.2
>
Thanks for the great package!
lcolladotor commented
Awesome! You did fix it as shown below.
> library(ggplot2)
> df <- data.frame(width = c(2806, 320, 287, 132, 214, 202, 235, 266, 156, 210),
+ seqnames=factor(c("chr20", "chr1", "chr6", "chr16", "chr17", "chr4", "chr1", "chr19", "chrX", "chr17")))
> df
width seqnames
1 2806 chr20
2 320 chr1
3 287 chr6
4 132 chr16
5 214 chr17
6 202 chr4
7 235 chr1
8 266 chr19
9 156 chrX
10 210 chr17
> png('test_ggplot.png')
> ggplot(df, aes(x=log10(width), colour=seqnames)) + geom_line(stat="density")
> dev.off()
null device
1
> devtools::session_info()
Session info -------------------------------------------------------------------------------------------------------------
setting value
version R version 3.2.0 Patched (2015-05-18 r68382)
system x86_64, darwin10.8.0
ui AQUA
language (EN)
collate en_US.UTF-8
tz America/New_York
Packages -----------------------------------------------------------------------------------------------------------------
package * version date source
bitops 1.0-6 2013-08-17 CRAN (R 3.2.0)
colorspace 1.2-6 2015-03-11 CRAN (R 3.2.0)
devtools 1.8.0 2015-05-09 CRAN (R 3.2.0)
digest 0.6.8 2014-12-31 CRAN (R 3.2.0)
ggplot2 * 1.0.1.9000 2015-06-12 Github (hadley/ggplot2@3b6a126)
git2r 0.10.1 2015-05-07 CRAN (R 3.2.0)
gtable 0.1.2 2012-12-05 CRAN (R 3.2.0)
labeling 0.3 2014-08-23 CRAN (R 3.2.0)
magrittr 1.5 2014-11-22 CRAN (R 3.2.0)
MASS 7.3-40 2015-03-21 CRAN (R 3.2.0)
memoise 0.2.1 2014-04-22 CRAN (R 3.2.0)
munsell 0.4.2 2013-07-11 CRAN (R 3.2.0)
plyr 1.8.2 2015-04-21 CRAN (R 3.2.0)
proto 0.3-10 2012-12-22 CRAN (R 3.2.0)
Rcpp 0.11.6 2015-05-01 CRAN (R 3.2.0)
RCurl 1.95-4.6 2015-04-24 CRAN (R 3.2.0)
reshape2 1.4.1 2014-12-06 CRAN (R 3.2.0)
rversions 1.0.0 2015-04-22 CRAN (R 3.2.0)
scales 0.2.4 2014-04-22 CRAN (R 3.2.0)
stringi 0.4-1 2014-12-14 CRAN (R 3.2.0)
stringr 1.0.0 2015-04-30 CRAN (R 3.2.0)
XML 3.98-1.2 2015-05-31 CRAN (R 3.2.0)
>