[R] ggplot2 stat_density2d issue.

David Winsemius dwinsemius at comcast.net
Thu Apr 19 02:16:46 CEST 2012


On Apr 18, 2012, at 7:52 PM, Dmitriy Lyubimov wrote:

> Sorry. i forgot the lines with parameters for my multivariate normal
> simulation. here's the code for data simulation i used:
>
> =======================
> sigm1 <- diag(c(30,50))
> sigm2 <- diag(c(5,3))
> mu1 <- c(10,15)
> mu2 <- c(80,60)
>
> 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")
> ========================
>
> On Wed, Apr 18, 2012 at 4:46 PM, Dmitriy Lyubimov <dlieu. 
> 7 at gmail.com> wrote:
>> 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.


The code is:
dens <- safe.call(kde2d, c(df, n = n, ...))
df <- with(dens, data.frame(expand.grid(x = x, y = y), z =  
as.vector(z)))
   The plot area gets (artificially) expanded to larger values. But it  
appears that the processing of "x" and "y" results in trimming to the  
range of the data despite efforts o extend it. Perhaps you should  
email Hadley?


>>
>> 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))

I don't know about 'h',  but the 'bins' parameter seems to be  
effective when changed from 4 to 20.

>>
>> +++++++++++++++
>>
>> 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
>>>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list