Hi listers, I want to make lattice plots xyplots with the indication of legends inside each panel with only the points and the lines actually ploted inside each given panel according to the group(ing) factor. The code below shows what I have achieved so far and I hope will make clear what I want to have. It seems to me that my solution is a very "dirty hack" and there certainly is a much simple and "clean" way to do it. Besides, there is no concordance in lty and pch between the legend above the graph with those inside the panels. I have searched the archive and the web and found some hints that helped me a lot to write the code below. But I am not very familiar with lattice trellis graphs and I am sure that I am missing something. I would appreciate any suggestion that will help me to improve this. Thank you in advance, Tito require(lattice) require(grid) # some random data lbt <- abs(rnorm(100)) # any biological value de <- rep(1:10,10) # the depth type <- rep(c("A","C","F","S"), c(20,30,40,10)) # the type of sampling zone sta <- as.factor(rep(1:10, c(10,10,10,10,10,10,10,10,10,10))) # the station number dataf <- data.frame(lbt,de,type,sta) # the dataframe # key creation keyA <- list(border = FALSE, text = list(levels(dataf$sta)[1:2]),lines=TRUE,points=TRUE,lty=c(1:2),pch=c(1:2)) keyC <- list(border = FALSE, text = list(levels(dataf$sta)[3:5]),lines=TRUE,points=TRUE,lty=c(3:5),pch=c(3:5)) keyF <- list(border = FALSE, text = list(levels(dataf$sta)[6:9]),lines=TRUE,points=TRUE,lty=c(6:9),pch=c(6:9)) keyS <- list(border = FALSE, text = list(levels(dataf$sta)[10]),lines=TRUE,points=TRUE,lty=c(10),pch=c(10)) # the plot xyplot(lbt ~ de | type, data=dataf, groups = sta,type=c("p","g","r"),layout=c(4,1),par.settings = standard.theme(color = FALSE), auto.key=list(space="top", columns=5, lines=TRUE)) # add "legends" inside the panels trellis.focus("panel", 1, 1) ; draw.key(keyA, draw = TRUE, vp = viewport(.25, .9)) trellis.focus("panel", 2, 1) ; draw.key(keyC, draw = TRUE, vp = viewport(.75, .9)) trellis.focus("panel", 3, 1) ; draw.key(keyF, draw = TRUE, vp = viewport(.75, .9)) trellis.focus("panel", 4, 1) ; draw.key(keyS, draw = TRUE, vp = viewport(.75, .9)) trellis.unfocus() -- Luis Tito de Morais IRD - UMR LEMAR (IRD/UBO/CNRS/IFREMER) Tél. : +33 2 98 49 86 35 Mob. : +33 6 37 67 21 40 Courriel-1 : Luis.Tito-de-Morais@ird.fr Courriel-2 : Luis.Titodemorais@univ-brest.fr Pages web : Lab. : http://www-iuem.univ-brest.fr/UMR6539/recherche/equipe-5 Pers. : http://ird.academia.edu/LuisTitodeMorais Reshal : http://www.netvibes.com/reshal#Accueil Biblio. : http://www.citeulike.org/user/ltitodem [[alternative HTML version deleted]]
On Sat, Jan 26, 2013 at 10:26 AM, Tito de Morais Luis < Luis.Tito-de-Morais@ird.fr> wrote:> Hi listers, > > I want to make lattice plots xyplots with the indication of legends > inside each panel with only the points and the lines actually ploted > inside each given panel according to the group(ing) factor. > > The code below shows what I have achieved so far and I hope will make > clear what I want to have. > It seems to me that my solution is a very "dirty hack" and there > certainly is a much simple and "clean" way to do it. > Besides, there is no concordance in lty and pch between the legend above > the graph with those inside the panels. > > No. Look again. It is your panel legends that don't correspond to theactual plot. The plot symbols and line types for the chosen theme !pch[1:10] and lty[1:10]. You can either explicitly set the pch and lty in the plot and auto.key to be 1:10 and proceed with trellis.focus or insert draw.key in the panel function to automate the procedure and query the groups and graphical parameters of each panel : xyplot(lbt ~ de | type, data=dataf, groups =sta, type=c("p","g","r"), layout=c(4,1), par.settings = standard.theme(color FALSE), auto.key=list(space="top", columns=5, lines=TRUE), panel=function(x,y,groups,subscripts,...){ panel.xyplot(x,y,groups=groups,subscripts=subscripts,...) pug <- levels(groups)[levels(groups)%in%groups[subscripts]] draw.key(key=list(text = list(as.character(pug)), lines = list(lty rep(trellis.par.get('superpose.line')$lty,10)[as.numeric(pug)]), points = list(pch rep(trellis.par.get('superpose.symbol')$pch,10)[as.numeric(pug)]) ), draw=TRUE , vp=viewport(x=0.25,y=0.9)) }) HTH> I have searched the archive and the web and found some hints that helped > me a lot to write the code below. But I am not very familiar with > lattice trellis graphs and I am sure that I am missing something. > I would appreciate any suggestion that will help me to improve this. > > Thank you in advance, > > Tito > > require(lattice) > require(grid) > # some random data > lbt <- abs(rnorm(100)) # any biological value > de <- rep(1:10,10) # the depth > type <- rep(c("A","C","F","S"), c(20,30,40,10)) # the type of sampling zone > sta <- as.factor(rep(1:10, c(10,10,10,10,10,10,10,10,10,10))) # the > station number > dataf <- data.frame(lbt,de,type,sta) # the dataframe > # key creation > keyA <- list(border = FALSE, text > list(levels(dataf$sta)[1:2]),lines=TRUE,points=TRUE,lty=c(1:2),pch=c(1:2)) > keyC <- list(border = FALSE, text > list(levels(dataf$sta)[3:5]),lines=TRUE,points=TRUE,lty=c(3:5),pch=c(3:5)) > keyF <- list(border = FALSE, text > list(levels(dataf$sta)[6:9]),lines=TRUE,points=TRUE,lty=c(6:9),pch=c(6:9)) > keyS <- list(border = FALSE, text > list(levels(dataf$sta)[10]),lines=TRUE,points=TRUE,lty=c(10),pch=c(10)) > > # the plot > xyplot(lbt ~ de | type, data=dataf, groups > sta,type=c("p","g","r"),layout=c(4,1),par.settings > standard.theme(color = FALSE), auto.key=list(space="top", columns=5, > lines=TRUE)) > > # add "legends" inside the panels > trellis.focus("panel", 1, 1) ; draw.key(keyA, draw = TRUE, vp > viewport(.25, .9)) > trellis.focus("panel", 2, 1) ; draw.key(keyC, draw = TRUE, vp > viewport(.75, .9)) > trellis.focus("panel", 3, 1) ; draw.key(keyF, draw = TRUE, vp > viewport(.75, .9)) > trellis.focus("panel", 4, 1) ; draw.key(keyS, draw = TRUE, vp > viewport(.75, .9)) > trellis.unfocus() > > > -- > Luis Tito de Morais > IRD - UMR LEMAR (IRD/UBO/CNRS/IFREMER) > > Tél. : +33 2 98 49 86 35 > Mob. : +33 6 37 67 21 40 > > Courriel-1 : Luis.Tito-de-Morais@ird.fr > Courriel-2 : Luis.Titodemorais@univ-brest.fr > > Pages web : > Lab. : http://www-iuem.univ-brest.fr/UMR6539/recherche/equipe-5 > Pers. : http://ird.academia.edu/LuisTitodeMorais > Reshal : http://www.netvibes.com/reshal#Accueil > Biblio. : http://www.citeulike.org/user/ltitodem > > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >[[alternative HTML version deleted]]
Thank you _very much_ Ilai for the rapid and accurate answer. It works and indeed helps a lot. Both to solve the question and to help me progress ! Possibly this will also help others. Thanks again Tito Le 27/01/2013 01:11, ilai a écrit :> > > On Sat, Jan 26, 2013 at 10:26 AM, Tito de Morais Luis > <Luis.Tito-de-Morais@ird.fr <mailto:Luis.Tito-de-Morais@ird.fr>> wrote: > > Hi listers, > > I want to make lattice plots xyplots with the indication of legends > inside each panel with only the points and the lines actually ploted > inside each given panel according to the group(ing) factor. > > The code below shows what I have achieved so far and I hope will make > clear what I want to have. > It seems to me that my solution is a very "dirty hack" and there > certainly is a much simple and "clean" way to do it. > Besides, there is no concordance in lty and pch between the legend > above > the graph with those inside the panels. > > No. Look again. It is your panel legends that don't correspond to the > actual plot. The plot symbols and line types for the chosen theme != > pch[1:10] and lty[1:10]. You can either explicitly set the pch and lty > in the plot and auto.key to be 1:10 and proceed with trellis.focus or > insert draw.key in the panel function to automate the procedure and > query the groups and graphical parameters of each panel : > > xyplot(lbt ~ de | type, data=dataf, groups =sta, > type=c("p","g","r"), layout=c(4,1), par.settings = > standard.theme(color = FALSE), > auto.key=list(space="top", columns=5, lines=TRUE), > panel=function(x,y,groups,subscripts,...){ > panel.xyplot(x,y,groups=groups,subscripts=subscripts,...) > pug <- levels(groups)[levels(groups)%in%groups[subscripts]] > draw.key(key=list(text = list(as.character(pug)), > lines = list(lty = > rep(trellis.par.get('superpose.line')$lty,10)[as.numeric(pug)]), > points = list(pch = > rep(trellis.par.get('superpose.symbol')$pch,10)[as.numeric(pug)]) > ), > draw=TRUE , vp=viewport(x=0.25,y=0.9)) > }) > > HTH > (...snip...)-- Luis Tito de Morais IRD - UMR LEMAR (IRD/UBO/CNRS/IFREMER) [[alternative HTML version deleted]]