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
<<dispersal_infestation.csv>>
######################################
## 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.Chen at gov.bc.ca
5th Floor -727 Fisgard Street
Victoria, BC V8W 1N1
P.O.Box 9519 Stn. Prov. Govt.
Victoria, BC V8W 9C2
=============================
Huapeng, It sounds like you have different y scales on the two plots you want to overlay? If so you could try doubleYScale() from the latticeExtra package. Please post a minimal, reproducible example (with data-generating code that can be pasted in to R) if you want more help. Regards -Felix On 20 April 2010 07:34, Chen, Huapeng FOR:EX <Huapeng.Chen at gov.bc.ca> wrote:> 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 > > > ?<<dispersal_infestation.csv>> > > ###################################### > ## 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.Chen at 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 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 / ??? Postdoctoral Fellow 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/
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
########################
District Year SPG_PER SPP_PER LDP_PER SPP_LDP_PER Infestation_NUM AI
DAB 1999 0.446808511 0.205673759 0.34751773 0.553191489 461 48.9143
DAB 2000 0.480769231 0.192307692 0.326923077 0.519230769 332 37.3591
DAB 2001 0.366141732 0.212598425 0.421259843 0.633858268 663 37.3418
DAB 2002 0.663366337 0.222772277 0.113861386 0.336633663 791 41.7671
DAB 2003 0.553278689 0.178278689 0.268442623 0.446721311 2138 54.333
DAB 2004 0.799204771 0.115308151 0.085487078 0.200795229 2921 62.5666
DAB 2005 0.824295011 0.106290672 0.069414317 0.175704989 2783 57.9458
DAB 2006 0.794520548 0.131506849 0.073972603 0.205479452 2210 50.0829
DAB 2007 0.77972028 0.148601399 0.071678322 0.22027972 2466 54.0918
DCC 1999 0.844036697 0.105504587 0.050458716 0.155963303 2077 57.9
DCC 2000 0.7731569 0.096408318 0.130434783 0.2268431 4294 75.5286
DCC 2001 0.905714286 0.065714286 0.028571429 0.094285714 4302 74.709
DCC 2002 0.799256506 0.066914498 0.133828996 0.200743494 6095 80.5838
DCC 2003 0.896703297 0.065934066 0.037362637 0.103296703 7919 85.7303
DCC 2004 0.887254902 0.068627451 0.044117647 0.112745098 8516 84.7028
DCC 2005 0.923728814 0.06779661 0.008474576 0.076271186 7759 79.4637
DCC 2006 0.941935484 0.04516129 0.012903226 0.058064516 6637 74.5994
DCC 2007 0.890909091 0.072727273 0.036363636 0.109090909 4288 68.593
DCH 1999 0.691176471 0.161764706 0.147058824 0.308823529 204 38.2682
DCH 2000 0.245901639 0.151639344 0.602459016 0.754098361 1099 55.3403
DCH 2001 0.525316456 0.189873418 0.284810127 0.474683544 1350 52.3413
DCH 2002 0.326599327 0.111111111 0.562289562 0.673400673 9332 88.5776
DCH 2003 0.797342193 0.102990033 0.099667774 0.202657807 11416 93.0988
DCH 2004 0.720430108 0.134408602 0.14516129 0.279569892 13120 94.1959
DCH 2005 0.773480663 0.066298343 0.160220994 0.226519337 13726 93.0554
DCH 2006 0.943319838 0.032388664 0.024291498 0.056680162 14393 92.2483
DCH 2007 0.961038961 0.019480519 0.019480519 0.038961039 13729 90.2417
DCK 1999 0.611111111 0.055555556 0.333333333 0.388888889 75 64.9573
DCK 2000 0.48 0.04 0.48 0.52 109 60.2273
DCK 2001 0.511111111 0.111111111 0.377777778 0.488888889 185 56.1514
DCK 2002 0.6875 0.09375 0.21875 0.3125 202 65.8192
DCK 2003 0.649122807 0.052631579 0.298245614 0.350877193 332 61.6667
DCK 2004 1 0 0 0 315 67.156
DCK 2005 0.541353383 0.157894737 0.30075188 0.458646617 603 57.967
DCK 2006 0.685314685 0.111888112 0.202797203 0.314685315 912 59.3861
DCK 2007 0.9 0.075 0.025 0.1 639 55.8722
############################################################################