Re: [R] Overlay of barchart and xyplot
Thanks a lot! Huapeng -Original Message- From: foolish.andr...@gmail.com [mailto:foolish.andr...@gmail.com] On Behalf Of Felix Andrews Sent: Friday, June 11, 2010 8:23 PM To: Chen, Huapeng FOR:EX Cc: r-help@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 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&qu
Re: [R] Overlay of barchart and xyplot
Hello, I am new to R. I am trying to use “DoubleYScale” to overlay one barchart with a xyplot with two lines. I have two problems. One is that I could not figure out how to custom line (point and line) with “par.settings” and seems to me that whatever “par.settings” I used to custom the barchart was also applied to the xyplot (lines) and I don’t know how to apply par.setting separately to each barchart and xyplot for my customization. The other problem is how to add a key for both barchart and xyplot in a meaningful way. I attached a piece of R code below I have been struggling to get this to work. Your kind help and advice are greatly appreciated. Thanks, Huapeng ### 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")), #auto.key=TRUE ) vars<-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)) ) doubleYScale(dispersal, vars,add.ylab2 = TRUE, #auto.key=list(text=c("LDP","SPP", "SPG","Infestation","Landscape aggregation index"), # points = list(pch=c(16,17)), lines=list(lty=c(1,2)), # rectangles = list(size=1.7,border="black", col=c("black", "light gray", "white")), # space = "right") ) update(trellis.last.object(), par.settings = simpleTheme(col = c("dark gray", "light gray", "white"),pch=c(16,17),lty=c(1,2))) ### ## A sample of data used DistrictYearSPG_PER SPP_PER LDP_PER SPP_LDP_PER Infestation_NUM AI DAB 19990.446808511 0.205673759 0.34751773 0.553191489 461 48.9143 DAB 20000.480769231 0.192307692 0.326923077 0.519230769 332 37.3591 DAB 20010.366141732 0.212598425 0.421259843 0.633858268 663 37.3418 DAB 20020.663366337 0.222772277 0.113861386 0.336633663 791 41.7671 DAB 20030.553278689 0.178278689 0.268442623 0.446721311 213854.333 DAB 20040.799204771 0.115308151 0.085487078 0.200795229 292162.5666 DAB 20050.824295011 0.106290672 0.069414317 0.175704989 278357.9458 DAB 20060.794520548 0.131506849 0.073972603 0.205479452 221050.0829 DAB 20070.77972028 0.148601399 0.071678322 0.22027972 246654.0918 DCC 19990.844036697 0.105504587 0.050458716 0.155963303 207757.9 DCC 20000.7731569 0.096408318 0.130434783 0.2268431 429475.5286 DCC 20010.905714286 0.065714286 0.028571429 0.094285714 430274.709 DCC 20020.799256506 0.066914498 0.133828996 0.200743494 609580.5838 DCC 20030.896703297 0.065934066 0.037362637 0.103296703 791985.7303 DCC 20040.887254902 0.068627451 0.044117647 0.112745098 851684.7028 DCC 20050.923728814 0.06779661 0.008474576 0.076271186 775979.4637 DCC 20060.941935484 0.04516129 0.012903226 0.058064516 663774.5994 DCC 20070.890909091 0.072727273 0.036363636 0.109090909 428868.593 DCH 19990.691176471 0.161764706 0.147058824 0.308823529 204 38.2682 DCH 20000.245901639 0.151639344 0.602459016 0.754098361 109955.3403 DCH 20010.525316456 0.189873418 0.284810127 0.474683544 135052.3413 DCH 20020.326599327 0.1 0.562289562 0.673400673 933288.5776 DCH 20030.797342193 0.102990033 0.099667774 0.202657807 11416 93.0988 DCH 20040.720430108 0.134408602 0.14516129 0.279569892 13120 94.1959 DCH 20050.773480663 0.066298343 0.160220994 0.226519337 13726 93.0554 DCH 20060.943319838 0.032388664 0.024291498 0.056680162 14393 92.
[R] Overlay of barchart and xyplot
Hello R Folks, I am new to R. I have been struggling to overlay a barchart with a xyplot together on one plot but did not get this worked out. Any help and idea are greatly appreciated. I attached R scripts for barchart and xyplot below and also data I used. What I am trying to do is just to put the barchart and xyplot together on one plot. Huapeng <> ## ## barchart dispersal_infestation <- read.table('Z:/project/bcmpb/pattern_process/data/BCMPB_MODEL/R_stat/dis persal_infestation.csv', header=T, sep=",") attach(dispersal_infestation) dispersal_infestation$Year <- factor(dispersal_infestation$Year, levels = c("1999","2000","2001","2002","2003","2004","2005","2006","2007","2008") , ordered = TRUE) barchart(dispersal_infestation$SPG_PER*100 + dispersal_infestation$SPP_PER*100 + dispersal_infestation$SPP_PER*100 ~ dispersal_infestation$Year | dispersal_infestation$District, #data=dispersal_infestation, layout=c(4,7), stack=TRUE, ## ## ## xyplot attach(dispersal_infestation) dispersal_infestation$Year <- factor(dispersal_infestation$Year, levels = c("1999","2000","2001","2002","2003","2004","2005","2006","2007","2008") , ordered = TRUE) library(lattice) my.theme <- list( axis.components = list(left = list(tck = 0.5, pad1 = 1, pad2 = 2), top = list(tck = 0, pad1 = -1.3, pad2 = 0), right = list(tck = 0.5, pad1 = 1, pad2 = 2), bottom = list(tck = +0.5, pad1 = 1, pad2 = 2))) trellis.par.set(theme = my.theme) panel1 = function(x, y) { #panel.loess(x, y, lwd=2.5, col="black") panel.xyplot(x, y, pch=18, col="black", cex=1.5,type='b',lwd=2) } #xyplot(log(dispersal_infestation$COUNT) ~ dispersal_infestation$Year | dispersal_infestation$ORG_UNIT_N, xyplot(sqrt(dispersal_infestation$Infestation_NUM) ~ dispersal_infestation$Year | dispersal_infestation$District, data=dispersal_infestation, layout=c(5,5), strip = strip.custom( bg="light grey"), panel=panel1, xlab="Year", ylab="Square roots of number of infested cells", main="BC MPB infestation by forest districts") ### = Huapeng Chen Landscape Modeling Biologist Research Branch Ministry of Forests and Range Phone: 250-387-2710 Fax: 250-387-0046 Email: huapeng.c...@gov.bc.ca 5th Floor -727 Fisgard Street Victoria, BC V8W 1N1 P.O.Box 9519 Stn. Prov. Govt. Victoria, BC V8W 9C2 = __ R-help@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.