[R] ggplot2 stat_density2d issue.

Dmitriy Lyubimov dlieu.7 at gmail.com
Thu Apr 19 01:46:43 CEST 2012


Thanks David.

let's say sample data (two-modal multivariate normal simulation, needs
mvtnorm package)

=========================
library(mvtnorm)

sample <- round(rbind(rmvnorm(100,mu1,sigm1),rmvnorm(100,mu2,sigm2)))
sample <- sample[! (sample[,1] <1 | sample[,2] <1 | sample[,1]> 100 |
sample[,2]>100),]
d <- as.data.frame(sample)
names(d)<-c("x","y")
=============

Now suppose we want to create ggplot2 density with this in a specified range

===============
library(ggplot2)
ggplot(d,aes(x,y)) +
		xlim(0,100)+ylim(0,100)+
		stat_density2d(bins=4)
===============

in my setup (which I believe is latest) i see incomplete contours of
the area limited by the range of the data. same is true if i switch
from contours to tile "heatmap" method in the help.

Issue #2. if we can pass kde2d parameters to stat_density2d, as my
documentation seems to imply, specifying bandwidths doesn't seem to
make any difference:

+++++++++++++++
ggplot(d,aes(x,y)) +
		xlim(0,100)+ylim(0,100)+
		stat_density2d(bins=4,
                       h=c(10,10))

+++++++++++++++

Thanks.
-dmitriy



On Wed, Apr 18, 2012 at 4:31 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Apr 18, 2012, at 6:55 PM, Dmitriy Lyubimov wrote:
>
>> Hello,
>>
>> I'd be very grateful for help with some ggplot2's stat_density2d issues.
>>
>> First issue is with data limits. xlim() and ylim() doesn't seem to
>> work; instead, estimates (and plotting) seems to be constrained to
>> range(x), range(y) no matter what i do. The documentation says i can
>> pass in kde2d's parameters to ... but pussing kde2d's "lims" parameter
>> achieves nothing. Contrary to the installed package help, the ggplot2
>> site reference says that "..." parameters are ignored. So does it or
>> does it not use kde2d? it seems not, but if not, how do i set the
>> limit?
>>
>> second question was about bandwidth. I want to override that (ideally,
>> similar to "adjust" parameter to stat::density but in case of kde2d,
>> "h" parameter would do as well). However, putting h=... doesn't seem
>> to have any effect, as as i mentioned before, it doesn't seem that
>> stat_density2d uses kde2d contrary to its doc.
>>
>> Could please somebody clarify what stat the ggplot2:: stat_density2d
>> is using now and how do i adjust bandwidth and limits in the current
>> version?
>
>
> https://github.com/hadley/ggplot2/blob/master/R/stat-density-2d.r
>
> (You might consider reading the Posting Guide. I am not pursuing further
> because I do not think it is our responsibility to construct test
> situations.)
>
> --
>
> David Winsemius, MD
> West Hartford, CT
>



More information about the R-help mailing list