[R] Drawing Polygons with xyplot in lattice
Josip Dasovic
jjd9 at sfu.ca
Tue Jan 13 02:16:45 CET 2009
Hello:
I've come to a dead-end in my search for a solution to a graphing problem that I am encountering. I have used xyplot (from the lattice package) successfully to plot 36 time-series plots (lines) of under-5 mortality for a set of countries in Sub-Sarahan Africa.
What I would now like to do is to add vertical bands (rectangles) that correspond to time-periods during which each of the 36 countries was at war, which would be different for each of the 36 panels. The effect I'd like to produce is similar that found here: http://thinkorthwim.com/2007/09/12/recession/ (but with 36 panels, each of which has a different period(s) shaded).
I've done this successfully one plot at a time with the simple plot commmand (using polygon after having first drawn the line plot), but am having difficulty replicating it with xyplot.
Here is some sample code (with 4 countries and 10 years), which is as far as I've been able to get:
library(lattice)
df<-data.frame(c(u5cmr=round(runif(40)*100)),country=c(rep("Angola",10), rep("Benin",10), rep("Botswana",10), rep("Burkina Faso",10)), year=rep(1991:2000,4))
names(df)<-c("u5cmr", "country", "year")
xyplot(u5cmr ~ year | country , ylab = "U5 cmr (per thousand)", data = df, type = "l", lty = 1, col ="red", xlab="Year", as.table=T, lwd=2)
R version 2.7.2 Patched (2008-09-20 r47259)
on Mac OS 10.5
Thanks,
Josip
More information about the R-help
mailing list