[R] Overlay of barchart and xyplot

Chen, Huapeng FOR:EX Huapeng.Chen at gov.bc.ca
Thu Jun 3 17:48:09 CEST 2010


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
############################################################################


More information about the R-help mailing list