Rebecca Fisher
2009-Apr-23 08:23 UTC
[R] generate individual panel keys/legends in lattice barchart
Dear R users.
I am wondering what is the simplest way is to generate individual keys for each
panel in a lattice barchart?
The help pages said: "To use more than one legend, or to have arbitrary
legends not constrained by the
structure imposed by key, use the legend argument", but after trying
several iterations, I could not get it to work.
any help you could give would be great,
Regards,
Rebecca.
############# code to reproduce my graph #####################
my_subcat_data <-
structure(list(subcat = structure(c(9L, 6L, 1L, 3L, 11L, 5L,
12L, 10L, 2L, 4L, 7L, 8L, 9L, 6L, 1L, 3L, 11L, 5L, 12L, 10L,
2L, 4L, 7L, 8L, 9L, 6L, 1L, 3L, 11L, 5L, 12L, 10L, 2L, 4L, 7L,
8L, 9L, 6L, 1L, 3L, 11L, 5L, 12L, 10L, 2L, 4L, 7L, 8L, 9L, 6L,
1L, 3L, 11L, 5L, 12L, 10L, 2L, 4L, 7L, 8L), .Label =
c("acidification",
"artesenal fishing", "bleaching", "commercial
fishing", "destructive fishing",
"eutrophication", "ocean warming", "oil spill",
"overfishing",
"recreational fishing", "sea level rise",
"sedimentation"), class = "factor"),
threat = structure(c(1L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 1L,
1L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 3L,
1L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 3L, 1L, 3L, 2L,
2L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 1L,
3L, 1L, 1L, 1L, 2L, 3L), class = "factor", .Label =
c("fishing",
"global warming", "pollution")), percent = c(1, 1, 1, 1,
1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1,
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 5, 2, 7, 6, 3,
8, 1, 1, 2, 2, 9, 4, 6, 9, 10, 2, 11, 8, 3, 1, 3, 5, 7),
decade = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1960 - 1970", "1970 -
1980",
"1980 - 1990", "1990 - 2000", "2000 - 2010"),
class = "factor")), .Names = c("subcat",
"threat", "percent", "decade"), row.names = c(NA,
-60L), class = "data.frame")
library(lattice)
subcat.plot=barchart(decade ~ percent | threat, data = my_subcat_data,
groups=subcat,
subscripts=TRUE,
stack=TRUE,
xlab=list("Percent of coral reef papers (%)",cex=1.3,font=2),
xlim=c(0,50),
ylab="",
auto.key = list(points = FALSE, rectangles = TRUE, space = "right"),
box.ratio = 2,
as.table=TRUE,
strip=strip.custom(bg="white",par.strip.text=list(font=2,cex=0.9)),
layout = c(1,length(levels(my_subcat_data$threat))),
scales=list(alternating=FALSE, cex=1,font=2,tck = c(1, 0)))
subcat.plot
------------------------------------------------------------
Dr Rebecca Fisher
Quantitative Ecologist
Australian Institute of Marine Science
Alternate email:rfisher at aims.gov.au
------------------------------------------------------------
Enjoy a better web experience. Upgrade to the new Internet Explorer 8
optimised for Yahoo!7. Get it now.
Deepayan Sarkar
2009-Apr-28 21:19 UTC
[R] generate individual panel keys/legends in lattice barchart
On Thu, Apr 23, 2009 at 1:23 AM, Rebecca Fisher <rebecca_fisher76 at yahoo.com.au> wrote:> > Dear R users. > I am wondering what is the simplest way is to generate individual keys for each panel in a lattice barchart? > The help pages said: "To use more than one legend, or to have arbitrary legends not constrained by the > structure imposed by key, use the legend argument", but after trying several iterations, I could not get it to work. > any help you could give would be great,Write a custom panel function, and call draw.key() inside. See the code for Figure 12.1 in http://lmdvr.r-forge.r-project.org/figures/figures.html for an example you can modify. -Deepayan> Regards, > Rebecca. > > > ############# code to reproduce my graph ##################### > my_subcat_data <- > structure(list(subcat = structure(c(9L, 6L, 1L, 3L, 11L, 5L, > 12L, 10L, 2L, 4L, 7L, 8L, 9L, 6L, 1L, 3L, 11L, 5L, 12L, 10L, > 2L, 4L, 7L, 8L, 9L, 6L, 1L, 3L, 11L, 5L, 12L, 10L, 2L, 4L, 7L, > 8L, 9L, 6L, 1L, 3L, 11L, 5L, 12L, 10L, 2L, 4L, 7L, 8L, 9L, 6L, > 1L, 3L, 11L, 5L, 12L, 10L, 2L, 4L, 7L, 8L), .Label = c("acidification", > "artesenal fishing", "bleaching", "commercial fishing", "destructive fishing", > "eutrophication", "ocean warming", "oil spill", "overfishing", > "recreational fishing", "sea level rise", "sedimentation"), class = "factor"), > ? ?threat = structure(c(1L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, > ? ?1L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 3L, > ? ?1L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, > ? ?2L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 1L, > ? ?3L, 1L, 1L, 1L, 2L, 3L), class = "factor", .Label = c("fishing", > ? ?"global warming", "pollution")), percent = c(1, 1, 1, 1, > ? ?1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, > ? ?1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 5, 2, 7, 6, 3, > ? ?8, 1, 1, 2, 2, 9, 4, 6, 9, 10, 2, 11, 8, 3, 1, 3, 5, 7), > ? ?decade = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, > ? ?1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, > ? ?3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, > ? ?4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, > ? ?5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1960 - 1970", "1970 - 1980", > ? ?"1980 - 1990", "1990 - 2000", "2000 - 2010"), class = "factor")), .Names = c("subcat", > "threat", "percent", "decade"), row.names = c(NA, -60L), class = "data.frame") > > library(lattice) > > subcat.plot=barchart(decade ~ percent | threat, data = my_subcat_data, > groups=subcat, > subscripts=TRUE, > stack=TRUE, > xlab=list("Percent of coral reef papers (%)",cex=1.3,font=2), > xlim=c(0,50), > ylab="", > auto.key = list(points = FALSE, rectangles = TRUE, space = "right"), > box.ratio = 2, > as.table=TRUE, > strip=strip.custom(bg="white",par.strip.text=list(font=2,cex=0.9)), > layout = c(1,length(levels(my_subcat_data$threat))), > scales=list(alternating=FALSE, cex=1,font=2,tck = c(1, 0))) > subcat.plot > > ?------------------------------------------------------------ > Dr Rebecca Fisher > Quantitative Ecologist > Australian Institute of Marine Science > Alternate email:rfisher at aims.gov.au > ------------------------------------------------------------ > > > > ? ? ?Enjoy a better web experience. Upgrade to the new Internet Explorer 8 optimised for Yahoo!7. Get it now. > > ______________________________________________ > 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. >