Why does smoothScatter clip when xlim and ylim increased?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does smoothScatter clip when xlim and ylim increased?

Jennifer Lyon
Hi:

Is there a way to get smoothScatter to not clip when I increase the xlim and
ylim parameters?
Consider the following example:

set.seed(17)
x1<-rnorm(100)
x2<-rnorm(100)
smoothScatter(x1,x2)

#Now if I increase xlim and ylim notice that the plot seems to be clipped at
the former xlim, and ylim boundaries:

smoothScatter(x1,x2, xlim=c(-5,5), ylim=c(-5,5))

Thanks.

Jen


sessionInfo()
R version 2.10.1 (2009-12-14)
i686-pc-linux-gnu

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  utils     datasets  grDevices methods   base

other attached packages:
[1] RColorBrewer_1.0-2

loaded via a namespace (and not attached):
[1] KernSmooth_2.23-3

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Why does smoothScatter clip when xlim and ylim increased?

Duncan Murdoch
On 06/02/2010 7:51 AM, Jennifer Lyon wrote:
 > Hi:
 >
 > Is there a way to get smoothScatter to not clip when I increase the
xlim and
 > ylim parameters?
 > Consider the following example:
 >
 > set.seed(17)
 > x1<-rnorm(100)
 > x2<-rnorm(100)
 > smoothScatter(x1,x2)
 >
 > #Now if I increase xlim and ylim notice that the plot seems to be
clipped at
 > the former xlim, and ylim boundaries:
 >
 > smoothScatter(x1,x2, xlim=c(-5,5), ylim=c(-5,5))

If you follow the links on the help page, you'll see that smoothScatter
uses bkde2D, which has a range.x argument to control the range of the
smoothing.  The smoothScatter function never passes the xlim and ylim
values to bkde2D, only to the plotting functions, presumably because the
author expected you to use them to limit the range, not extend it.

You can get the behaviour you want with specified xlim and ylim by
modifying one line in smoothScatter:

map <- grDevices:::.smoothScatterCalcDensity(x, nbin, bandwidth)

should become

map <- grDevices:::.smoothScatterCalcDensity(x, nbin, bandwidth,
list(xlim, ylim))

(You can use fix(smoothScatter) to edit your own local copy of
smoothScatter and make this change.)

However, this messes up the default plot, so a better patch would be
needed to permanently fix this.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Why does smoothScatter clip when xlim and ylim increased?

Jennifer Lyon
On Sat, Feb 6, 2010 at 6:15 AM, Duncan Murdoch <[hidden email]> wrote:

> On 06/02/2010 7:51 AM, Jennifer Lyon wrote:
>> Hi:
>>
>> Is there a way to get smoothScatter to not clip when I increase the xlim
>> and
>> ylim parameters?
>> Consider the following example:
>>
>> set.seed(17)
>> x1<-rnorm(100)
>> x2<-rnorm(100)
>> smoothScatter(x1,x2)
>>
>> #Now if I increase xlim and ylim notice that the plot seems to be clipped
>> at
>> the former xlim, and ylim boundaries:
>>
>> smoothScatter(x1,x2, xlim=c(-5,5), ylim=c(-5,5))
>
> If you follow the links on the help page, you'll see that smoothScatter uses
> bkde2D, which has a range.x argument to control the range of the smoothing.
>  The smoothScatter function never passes the xlim and ylim values to bkde2D,
> only to the plotting functions, presumably because the author expected you
> to use them to limit the range, not extend it.
>
> You can get the behaviour you want with specified xlim and ylim by modifying
> one line in smoothScatter:
>
> map <- grDevices:::.smoothScatterCalcDensity(x, nbin, bandwidth)
>
> should become
>
> map <- grDevices:::.smoothScatterCalcDensity(x, nbin, bandwidth, list(xlim,
> ylim))
>
> (You can use fix(smoothScatter) to edit your own local copy of smoothScatter
> and make this change.)
>
> However, this messes up the default plot, so a better patch would be needed
> to permanently fix this.
>
> Duncan Murdoch
>
Ah. A very helpful explanation. Further exploration led to the
realization that if I passed in par("usr") instead of xlim and ylim
that both the case I care about and the default case display without
clipping. Of course I discovered (very reasonably) that par("usr")
doesn't exist until plot() is called, so I ended up calling plot and
then modifying the image call with add=T. Along the lines of:
 plot(NA,NA, xlab = xlab, ylab = ylab, xlim = xlim, ylim = ylim, xaxs
= xaxs, yaxs = yaxs, type="n", ...)
usr<-par("usr")
map <- grDevices:::.smoothScatterCalcDensity(x, nbin, bandwidth,
 list(usr[1:2],usr[3:4]))
...
image(xm, ym, z = dens, col = colramp(256), xlab = xlab,
 ylab = ylab, xlim = xlim, ylim = ylim, xaxs = xaxs, yaxs = yaxs,
add=T, ...)

This is somewhat wasteful, as bkde2D is computing densities at grid
points well away from where the data is located, so I'll just have to
increase the number of grid points.

Thank you for your help.

Jen

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.