[R] A question about pairs()

Felix Andrews felix at nfrac.org
Sun Nov 2 06:00:57 CET 2008


Ian,

pairs() is (normally) a base graphics function, *not* a trellis
graphics function. You can not easily mix the two graphics systems. So
you can not use panel.number(), panel.smooth(), etc with pairs.

There is a trellis graphics equivalent to pairs, called splom (in the
lattice package). I do not understand your example well enough to say
whether you would best use a pairs/splom type of plot, or just a
layout of simple plots.

-Felix


2008/10/31 Ian Sue Wing <isw at bu.edu>:
> Greetings R users,
>
> I am an R graphics newbie trying to produce a custom trellis plot using
> pairs() with R 2.7.2.
>
> I have spatial data on which I run a geographically weighted regression
> (gwr, using the -spgwr- package). I want to check the gwr coefficients for
> multicollinearity and spatial association, following Wheeler and Tiefelsdorf
> (2005), and I would like to summarize the results of this exercise using
> -pairs-.
>
> My problem is that I want to generate figures for two groups of variables
> and then assemble them into a panel display. The first set of variables is
> the gwr coefficients, for which I want to generate x-y scatterplots. This is
> straightforward, and works fine. The second set of variables is the local
> correlation between the gwr coefficient estimates, which I want to plot as
> kernel densities. Is there any way to do this? I can do one or the other,
> but not both together.
>
> For example:
>
> data(columbus)
> # compute gwr bandwidth
> col.bw <- gwr.sel(crime ~ income + housing, data=columbus,
> coords=cbind(columbus$x, columbus$y))
> # estimate gwr
> col.gauss <- gwr(crime ~ income + housing, data=columbus,
> coords=cbind(columbus$x, columbus$y), bandwidth=col.bw, hatmatrix=TRUE)
> # summarize gwr results
> col.gauss
> # compute local correlation matrix among gwr coefs
> col.cor <- gw.cov(col.gauss$SDF,vars=2:4,bw=col.bw,cor=T,longlat=F)
> # extract gwr coefficients and local correlations
> col.gauss.mat <- as(col.gauss$SDF, "data.frame")[,2:4]
> col.cor.mat <- as(col.cor$SDF, "data.frame")[,16:18]
> # define correlation function for use by -pairs-
> panel.cor <- function(x,y,digits=4)
> {
>   r = (cor(x,y,use="pairwise"))
>   txt <- format(c(r, 0.123456789), digits=digits)[1]
>   text(0.5, 0.5, txt)
> }
> # show gwr results: works fine, except that text positioning is screwed up
> pairs(col.gauss.mat,
>   upper.panel=panel.smooth,
>   lower.panel=panel.cor,
>   labels=c("Intercept","Income","Housing"))
> # show gwr local correlations: works fine, except that text positioning is
> screwed up
> pairs(col.cor.mat,
>   upper.panel=panel.smooth,
>   lower.panel=panel.cor,
>   labels=c("Intercept","Income","Housing"))
>
> # My problem is with the following function...
>
> panel.dis <- function(...)
> {
>   if(panel.number()==4) dist <- lines(density(col.cor.mat[,1]))
>   if(panel.number()==7) dist <- lines(density(col.cor.mat[,2]))
>   if(panel.number()==8) dist <- lines(density(col.cor.mat[,3]))
> }
>
> pairs(col.gauss.mat,
>   upper.panel=panel.smooth,
>   lower.panel=panel.dis,
>   labels=c("Intercept","Income","Housing"))
>
>
> The last -pairs- call terminates with the following error:
>
> Error in if (panel.number() == 4) dist <- lines(density(col.cor.mat[,  :
>  argument is of length zero
>
> I thought I could address panels in this way using the panel.number()
> function. I would very much appreciate it if someone could let me know what
> I am doing wrong. Should I not use -pairs- at all? The problem is that I'd
> like the plots to line up nicely.
>
> -i
>
> --
> Ian Sue Wing                      675 Commonwealth Ave., Boston MA 02215
> Associate Professor               Tel: (617) 353-5741
> Dept. of Geography & Environment  Fax: (617) 353-5986
> Boston University                 Web: http://people.bu.edu/isw
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Felix Andrews / 安福立
http://www.neurofractal.org/felix/
3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8



More information about the R-help mailing list