Hofert Marius
2008-Jan-01 11:36 UTC
[R] Alignment and Labeling of a color key in a xyplot?
Happy New Year to all R users! I have two short questions concerning a xyplot with a color key: 1) How do I properly place (align) the color key beside the xyplot? As you can see from the code listed below, the placement of the color key is not correct. I would like the upper and lower end point of the color key to be perfectly aligned with the upper and lower line of the xyplot. Adjusting the viewport-arguments below is tedious and also does not help if you want the print the plot in a postscript file (see the commented lines below). 2) How do I put a label on the color key? I would like to give the color key a label as the label 'x' for the y- axis, so it should be vertically printed and located to the right of the color key. I tried to give the 'key' argument in 'draw.colorkey' some arguments, but I could not manage to put a label on the color key. Thanks very much. Marius library(lattice) library(grid) x=c(1,2,3) colorseq=c(0.2,0.5,0.8) mycolors=gray(colorseq) #trellis.device(postscript,horizontal=F,onefile=F,file="~/testplot.ps") xyplot(x~x,col=mycolors,aspect=1) draw.colorkey(key=list(at=seq(1,3,length=4),col=mycolors,text=list(c ("test"))),draw=TRUE,vp=viewport(x=1,y=0.6,height=0.8)) #dev.off()
Deepayan Sarkar
2008-Jan-01 16:50 UTC
[R] Alignment and Labeling of a color key in a xyplot?
On 1/1/08, Hofert Marius <m_hofert at web.de> wrote:> Happy New Year to all R users! > > I have two short questions concerning a xyplot with a color key: > > 1) How do I properly place (align) the color key beside the xyplot? > As you can see from the code listed below, the placement of the color > key is not correct. I would like the upper and lower end point of the > color key to be perfectly aligned with the upper and lower line of > the xyplot. Adjusting the viewport-arguments below is tedious and > also does not help if you want the print the plot in a postscript > file (see the commented lines below).It's easier if you include the legend in the call (so that print.trellis can do the layout management); e.g., see http://lmdvr.r-forge.r-project.org/figures/figures.html?chapter=05;figure=05_06;theme=stdBW;code=right> 2) How do I put a label on the color key? > I would like to give the color key a label as the label 'x' for the y- > axis, so it should be vertically printed and located to the right of > the color key. I tried to give the 'key' argument in 'draw.colorkey' > some arguments, but I could not manage to put a label on the color key.This is not supported by draw.colorkey(). You can still define your own legend, of course. -Deepayan> > Thanks very much. > > Marius > > library(lattice) > library(grid) > x=c(1,2,3) > colorseq=c(0.2,0.5,0.8) > mycolors=gray(colorseq) > #trellis.device(postscript,horizontal=F,onefile=F,file="~/testplot.ps") > xyplot(x~x,col=mycolors,aspect=1) > draw.colorkey(key=list(at=seq(1,3,length=4),col=mycolors,text=list(c > ("test"))),draw=TRUE,vp=viewport(x=1,y=0.6,height=0.8)) > #dev.off() > > ______________________________________________ > 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. >
Gabor Grothendieck
2008-Jan-01 17:11 UTC
[R] Alignment and Labeling of a color key in a xyplot?
Is this what you want (same as your except the draw.key and reference to grid is removed key=... is added in the xyplot call: library(lattice) x <- 1:3 colorseq < c(0.2, 0.5, 0.8) mycolors <- gray(colorseq) xyplot(x ~ x, col = mycolors, aspect = 1, key = list(rect = list(col = mycolors), text = list(letters[1:3]), space = "right") ) On Jan 1, 2008 6:36 AM, Hofert Marius <m_hofert at web.de> wrote:> Happy New Year to all R users! > > I have two short questions concerning a xyplot with a color key: > > 1) How do I properly place (align) the color key beside the xyplot? > As you can see from the code listed below, the placement of the color > key is not correct. I would like the upper and lower end point of the > color key to be perfectly aligned with the upper and lower line of > the xyplot. Adjusting the viewport-arguments below is tedious and > also does not help if you want the print the plot in a postscript > file (see the commented lines below). > > 2) How do I put a label on the color key? > I would like to give the color key a label as the label 'x' for the y- > axis, so it should be vertically printed and located to the right of > the color key. I tried to give the 'key' argument in 'draw.colorkey' > some arguments, but I could not manage to put a label on the color key. > > Thanks very much. > > Marius > > library(lattice) > library(grid) > x=c(1,2,3) > colorseq=c(0.2,0.5,0.8) > mycolors=gray(colorseq) > #trellis.device(postscript,horizontal=F,onefile=F,file="~/testplot.ps") > xyplot(x~x,col=mycolors,aspect=1) > draw.colorkey(key=list(at=seq(1,3,length=4),col=mycolors,text=list(c > ("test"))),draw=TRUE,vp=viewport(x=1,y=0.6,height=0.8)) > #dev.off() > > ______________________________________________ > 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. >