6

stat_density2d seems don't like discrete data. My code following cannot be plotted, and have such errors on R 2.15.2:

Error in contourLines(x = sort(unique(data$x)), y = sort(unique(data$y)), : no proper 'z' matrix specified In addition: Warning messages: 1: Removed 10000 rows containing non-finite values (stat_contour). 2: In min(x) : no non-missing arguments to min; returning Inf 3: In max(x) : no non-missing arguments to max; returning -Inf

On R 2.15.3, the error is:

Error in function (x, y, h, n = 25, lims = c(range(x), range(y))) :
bandwidths must be strictly positive

Probably because of the limitation n = 25.

a<-url("http://www-personal.umich.edu/~ajing/Files/TestData.RData")
load(a)

library(ggplot2)
p1<-ggplot(myd, aes(x=xvar,y=yvar)) + stat_density2d(aes(fill=..level..), geom="polygon")
gt <- ggplot_gtable(ggplot_build(p1))

Anyway to avoid this error? Or other recommended 2D histogram plots?

As I need two marginal histograms, ggplot is better than filled.contour for me.

whatsnext
  • 617
  • 7
  • 19
  • 1
    I get a different error message, `bandwidths must be strictly positive` (from kde2d). There are no discrete data here. – mnel Mar 19 '13 at 23:26
  • OK, because you have a newer R version. My version is 2.15.2. 2.15.3 will give your error – whatsnext Mar 20 '13 at 03:10
  • It will be a `ggplot2` version issue more likely. – mnel Mar 20 '13 at 03:14
  • possible duplicate of [Scatterplot with marginal histograms in ggplot2](http://stackoverflow.com/questions/8545035/scatterplot-with-marginal-histograms-in-ggplot2) – mnel Mar 20 '13 at 03:17
  • Yes, I like contour plot with marginal histograms in this situation, but I have the above problem. Do you know how to catch warning message? – whatsnext Mar 20 '13 at 04:35
  • 1
    Update R and ggplot2! – mnel Mar 20 '13 at 04:48
  • How to upgrade ggplot2 for R in ubuntu? – whatsnext Mar 20 '13 at 13:02
  • 1
    What exactly do you expect a density plot to do with categorical inputs? Maybe you want `geom_tile` or `geom_bin2d`? – hadley Mar 20 '13 at 13:21
  • Even I upgrade both R and ggplot2, it still has "bandwidths must be strictly positive" error. – whatsnext Mar 20 '13 at 13:22
  • Not exactly categorical, but I would say it count data. geom_bin2d can work, but I want to see gradient details for 2d plot. geom_bin2d is too obscure. – whatsnext Mar 20 '13 at 19:01

0 Answers0