[R] segplot (latticeExtra)

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue May 25 11:05:04 CEST 2010


On Tue, May 25, 2010 at 8:37 AM, Sebastian P. Luque <spluque at gmail.com> wrote:
> Hi,
>
> I'm having a bit of trouble with 'scales="free"' in the segplot()
> function of latticeExtra.  Say we need panels for each year, showing
> only those counties that are represented in each one:
>
> ---<--------------------cut here---------------start------------------->---
> library(latticeExtra)
> data(USCancerRates)
> uscr.w <- subset(USCancerRates, state == "Washington")
> uscr.w$year <- gl(4, 10, length=nrow(uscr.w), labels=format(seq(2001, 2004)))
>
> segplot(reorder(factor(county), rate.male) ~ LCL95.male + UCL95.male | year,
>        data=uscr.w, scales="free")
> ---<--------------------cut here---------------end--------------------->---
>
> This still plots all levels of county in every panel.  Based on a
> similar need for dotplot in another thread, the following looked
> promising, but gives empty panels:
>
> ---<--------------------cut here---------------start------------------->---
> segplot(reorder(factor(county), rate.male) ~ LCL95.male + UCL95.male | year,
>        data=uscr.w, scales="free",
>        prepanel=function(x, y, z, ...) {
>            zz <- z[, drop=TRUE]
>            list(sort(unique(zz)))
>        },
>        panel=function(x, y, z, ...) {
>            zz <- z[, drop=TRUE]
>            panel.segplot(x, y, zz, ...)
>        })
> ---<--------------------cut here---------------end--------------------->---
>
> Is it possible to have only counties for the particular panel (year) in
> these plots?  Thanks.

The default prepanel function does not provide enough information (I
have been meaning to fix this). Specifically, it needs to return the
locations that are present:

segplot(reorder(factor(county), rate.male) ~ LCL95.male + UCL95.male | year,
        data=uscr.w, scales=list(relation = "free", y = list(rot = 0)),
        prepanel = function(x, y, z, ..., subscripts) {
            ans <- prepanel.segplot(x, y, z, ..., subscripts = subscripts)
            if (is.factor(z))
                ans$yat <- sort(unique(as.numeric(z[subscripts])))
            ans$xat <- NULL
            ans
        })

-Deepayan



More information about the R-help mailing list