[R] Overlay of barchart and xyplot

Chen, Huapeng FOR:EX Huapeng.Chen at gov.bc.ca
Mon Jun 14 19:22:11 CEST 2010


Thanks a lot!

Huapeng

-----Original Message-----
From: foolish.android at gmail.com [mailto:foolish.android at gmail.com] On Behalf Of Felix Andrews
Sent: Friday, June 11, 2010 8:23 PM
To: Chen, Huapeng FOR:EX
Cc: r-help at r-project.org
Subject: Re: [R] Overlay of barchart and xyplot

Hi,

I have an example below of adding a key to the merged plot.

You can not have the key on the right hand side because that viewport
is used by the second ylab (ylab2 from doubleYScale). Well, if you
really wanted to, you could do it with the grid package, using
frameGrob or somesuch.


NTLST_Dispersal_VAR_00_08$Year <-
factor(NTLST_Dispersal_VAR_00_08$Year, levels =
c("1999","2000","2001","2002","2003","2004","2005","2006","2007"),
ordered = TRUE)

dispersal<-
barchart(LDP_PER*100 + SPP_PER*100 + SPG_PER*100 ~
                    Year | District,
                    data=NTLST_Dispersal_VAR_00_08,
                    stack=TRUE,
                    layout=c(5,5),
                    scales = list(x = list(rot = 90)),
                    xlab="Year", ylab="%",
                    strip = strip.custom( bg="light gray"),
                    par.settings = simpleTheme(col = c("dark gray",
"light gray", "white")),
                    auto.key = list(points = FALSE, rectangles = TRUE)
                    )


vars <-
xyplot(sqrt(Infestation_NUM) + AI  ~  Year | District,
            data=NTLST_Dispersal_VAR_00_08,
            layout=c(5,5),
            type="b",
            ylab="Square roots of number of infested cells/Landscape
aggregation index",
            auto.key = list(lines = TRUE)
            )

dblplot <-
doubleYScale(dispersal, vars, use.style=FALSE, add.ylab2 = TRUE
            )

dblplot <-
update(dblplot,
    par.settings = simpleTheme(fill = c("white", "dark gray",
"black"), border="black",col.line="black",
                           col.points="black",pch=c(16,17),lty=c(1,1,1,2,1))
)

## include second key at the bottom
update(dblplot, legend = list(bottom = vars$legend$top))

## Otherwise you could just include a "key" argument in the first plot
which includes all the items explicitly.

## Or merge the two 'auto.key's at the top:

mergeLegends <- function(a, b, ...) {
    g <- frameGrob()
    agrob <- a
    if (!inherits(a, "grob")) {
        a <- eval(as.call(c(as.symbol(a$fun), a$args)), getNamespace("lattice"))
    }
    if (!inherits(b, "grob")) {
        b <- eval(as.call(c(as.symbol(b$fun), b$args)), getNamespace("lattice"))
    }
    g <- packGrob(g, a, side = "left")
    packGrob(g, b, side = "right")
}

update(dblplot, legend = list(top = list(fun = "mergeLegends",
    args = list(a = dispersal$legend$top, b = vars$legend$top))))





On 5 June 2010 04:49, Chen, Huapeng FOR:EX <Huapeng.Chen at gov.bc.ca> wrote:
> Hi Felix,
>
> Thanks for your help and advice. The following code is close to what I want but still have problems of failure to custom lines and add a key in any way. Par.settings with the final plot seems not working somehow except pch and lty but they overwrite par.setting with barchart. I also attached data I used by using "dput". I appreciate your further helps.
>
> Thanks,
>
> Huapeng
>
>
> ######### code #############################################################
> NTLST_Dispersal_VAR_00_08$Year <- factor(NTLST_Dispersal_VAR_00_08$Year, levels = c("1999","2000","2001","2002","2003","2004","2005","2006","2007"), ordered = TRUE)
>
> dispersal<-barchart(NTLST_Dispersal_VAR_00_08$LDP_PER*100 +
>                     NTLST_Dispersal_VAR_00_08$SPP_PER*100 +
>                     NTLST_Dispersal_VAR_00_08$SPG_PER*100 ~
>                     NTLST_Dispersal_VAR_00_08$Year | NTLST_Dispersal_VAR_00_08$District,
>                     data=NTLST_Dispersal_VAR_00_08,
>                     horizontal=FALSE,
>                     stack=TRUE,
>                     layout=c(5,5),
>                     xlab="Year",
>                     ylab="%",
>                     strip = strip.custom( bg="light gray"),
>                     par.settings = simpleTheme(col = c("dark gray", "light gray", "white")),
>                     #key=list(space="right",size=10,
>                     # rectangles=list(size=1.7, border="black", col = c("white", "dark gray", "black")),
>                      #lines=list(pch=c(16,17),lty=c(1,2),col="black",type="b"),
>                     # text=list(text=c("SPG","SPP","LDP")))
>
>                     #auto.key=TRUE
>                     )
>
>
>
> xyplot(sqrt(NTLST_Dispersal_VAR_00_08$Infestation_NUM) +
>             NTLST_Dispersal_VAR_00_08$AI  ~  NTLST_Dispersal_VAR_00_08$Year | NTLST_Dispersal_VAR_00_08$District,
>             data=NTLST_Dispersal_VAR_00_08,
>             layout=c(5,5),
>             type="b",
>             ylab="Square roots of number of infested cells/Landscape aggregation index",
>             #par.settings = simpleTheme(col = c("black", "black"), pch=c(16,17)),
>             #key=list(space="right",size=10,
>                      #rectangles=list(size=1.7, border="black", col = c("white", "dark gray", "black")),
>             #         lines=list(pch=c(16,17),lty=c(1,2),col="black",type="b"),
>             #         text=list(text=c("t4","t5")))
>
>             )
>
>  doubleYScale(dispersal, vars, use.style=FALSE, add.ylab2 = TRUE
>             )
>
>  update(trellis.last.object(),
>  par.settings = simpleTheme(fill = c("white", "dark gray", "black"), border="black",col.line="black",
>                            col.points="black",pch=c(16,17),lty=c(1,1,1,2,1)))
>
> ######################################################################################################
>





-- 
Felix Andrews / 安福立
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
-- 
http://www.neurofractal.org/felix/


More information about the R-help mailing list