[R] legend in lattice densityplot [Broadcast]

Albert Vilella avilella at gmail.com
Wed Feb 14 15:49:59 CET 2007


I am defining the legend using trellis.par.set (not sure if
correctly), and space does not seem to do the trick. auto-key (here
commented) places it to the top...

a = rep(c("alfa","beta","gamma","alfa","beta","gamma"),100)
b = rnorm(600)
input=data.frame(a,b)
densityplot(~(input$b),
  groups=input$a,
  plot.points=FALSE,
#  auto.key=TRUE,
  space = "left",
  trellis.par.set(superpose.line = list(
    col = rep( c("yellow","green","red","blue","orange","pink","lightblue","black","brown"),
3) ,
    lwd=3,
    lty = rep( c(1,2,3), each = 9) )
  )
)


On 2/14/07, Wiener, Matthew <matthew_wiener at merck.com> wrote:
> From the documentation for xyplot (referred to from densityplot):
>
> The position of the key can be controlled in either of two possible
> ways. If a component called space is present, the key is positioned
> outside the plot region, in one of the four sides, determined by the
> value of space, which can be one of "top", "bottom", "left" and "right".
>
>
> Hope this helps,
>
> Matt Wiener
>
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Albert Vilella
> Sent: Wednesday, February 14, 2007 8:46 AM
> To: R-help at stat.math.ethz.ch
> Subject: Re: [R] legend in lattice densityplot [Broadcast]
>
> How can I place the legend to the left or right of the densityplot? By
> default, it goes at the top, and as it is a rather long list, the
> density plot only uses half the space of the whole graphic...
>
> On 11/30/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > Me too on Windows XP.
> >
> > Its probably just a bug or unimplemented feature in the SVG driver.
> > Write to the maintainer of that package
> >
> > For a workaround generate fig output and then convert it to svg using
> whatever
> > fig editor or converter you have.
> >
> > (On my windows system I use the free fig2dev converter although it
> inserted
> > a DOCTYPE statement into the generated SVG file that IE7 did not
> recognize
> > but once I manually deleted that it displayed ok in IE7.)
> >
> > # after producing file01.fig run
> > #   fig2dev -L svg file01.fig file01.svg
> > # or use some other fig to svg converter or editor
> > xfig(file = "/file01.fig", onefile = TRUE)
> > library(lattice)
> > set.seed(1)
> > DF <- data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)),
> >        f = sample(c("A","B","C","D","E"),300,replace=TRUE))
> > densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE,
> >  par.settings = list(superpose.line = list(col = c(1,1,2,2), lty =
> 1:2,
> >  lwd = c(1,1,1,1,2))))
> > dev.off()
> >
> >
> > On 11/30/06, Albert Vilella <avilella at gmail.com> wrote:
> > > Should it be a problem to print this dashed line plots as svgs?
> > >
> > > library(RSvgDevice)
> > > devSVG(file = "/home/avilella/file01.svg",
> > >       width = 20, height = 16, bg = "white", fg = "black",
> onefile=TRUE,
> > >       xmlHeader=TRUE)
> > > densityplot(...)
> > > dev.off()
> > >
> > > I am getting all the lines as continuous, not dashed...
> > >
> > > On 11/30/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > > > Yes  by using the lty suboption of superpose.line.
> > > > Here is a modification of the prior example to illustrate:
> > > > We also use lwd as well in this example.
> > > >
> > > > set.seed(1)
> > > > DF <- data.frame(x =
> c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)),
> > > >        f = sample(c("A","B","C","D","E"),300,replace=TRUE))
> > > > library(lattice)
> > > > densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points =
> FALSE,
> > > >  par.settings = list(superpose.line = list(col = c(1,1,2,2), lty =
> 1:2,
> > > >  lwd = c(1,1,1,1,2))))
> > > >
> > > >
> > > > On 11/30/06, Albert Vilella <avilella at gmail.com> wrote:
> > > > > Can I combine colors and line types? For example, would it be
> possible
> > > > > to have 5 colors per 2 types of lines (continuous and dashed)?
> > > > >
> > > > > On 11/29/06, Chuck Cleland <ccleland at optonline.net> wrote:
> > > > > > Albert Vilella wrote:
> > > > > > > Are this legend colors correlated to the plot?
> > > > > >
> > > > > >   They are if you rely on the colors in
> > > > > >
> > > > > > trellis.par.get("superpose.line")$col
> > > > > >
> > > > > >   If you want different colors you might use trellis.par.set()
> to
> > > > > > temporarily change the colors:
> > > > > >
> > > > > > x <- c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1))
> > > > > > f <- rep(c("A","B","C"), each=100)
> > > > > > df <- data.frame(x,f)
> > > > > > library(lattice)
> > > > > >
> > > > > > oldpar <- trellis.par.get("superpose.line")$col
> > > > > >
> > > > > > trellis.par.set(superpose.line = list(col = heat.colors(3)))
> > > > > >
> > > > > > densityplot(~ x, groups = f, data = df,
> > > > > >                  plot.points=FALSE,
> > > > > >                  auto.key=TRUE)
> > > > > >
> > > > > > trellis.par.set(superpose.line = list(col = oldpar))
> > > > > >
> > > > > >   If you don't require points or lines in the key, you also
> could do
> > > > > > something like this:
> > > > > >
> > > > > > densityplot(~ x, groups = f, data = df,
> > > > > >                  plot.points=FALSE,
> > > > > >                  key = simpleKey(levels(df$f),
> > > > > >                                  lines=FALSE,
> > > > > >                                  points=FALSE,
> > > > > >                                  col=heat.colors(3)),
> > > > > >                  col=heat.colors(3))
> > > > > >
> > > > > >   To use your own colors without changing the trellis settings
> and to
> > > > > > get lines or points in the key, you probably need at least to
> use key =
> > > > > > simpleKey() rather than the auto.key argument, and you may
> need to look
> > > > > > into draw.key().  Other people on the list might know simpler
> approaches
> > > > > > for using your own colors in this situation.
> > > > > >
> > > > > > > If I do a:
> > > > > > >
> > > > > > > densityplot(~x, groups=f, plot.points=FALSE,
> > > > > > > auto.key=TRUE,col=heat.colors(5))
> > > > > > >
> > > > > > > I get different colors in the legend than the plot...
> > > > > > >
> > > > > > >
> > > > > > > On 11/29/06, Chuck Cleland <ccleland at optonline.net> wrote:
> > > > > > >> Albert Vilella wrote:
> > > > > > >> > Hi,
> > > > > > >> >
> > > > > > >> > I have a densityplot like this:
> > > > > > >> >
> > > > > > >> > x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6))
> > > > > > >> > f = sample(c("A","B","C","D","E"),300,replace=TRUE)
> > > > > > >> > df=data.frame(x,f)
> > > > > > >> > library(lattice)
> > > > > > >> > attach(df)
> > > > > > >> > densityplot(~x, groups=f)
> > > > > > >> >
> > > > > > >> > And I want to add a legend with the colours for the
> factors. How can
> > > > > > >> I do that?
> > > > > > >> > How can I not have the dots of the distribution at the
> bottom, or at
> > > > > > >> > least, make them occupy less vertical space?
> > > > > > >>
> > > > > > >>   Change the last line to the following:
> > > > > > >>
> > > > > > >> densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE)
> > > > > > >>
> > > > > > >> See ?panel.densityplot .
> > > > > > >>
> > > > > > >> > ______________________________________________
> > > > > > >> > R-help at stat.math.ethz.ch 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.
> > > > > > >> >
> > > > > > >>
> > > > > > >> --
> > > > > > >> Chuck Cleland, Ph.D.
> > > > > > >> NDRI, Inc.
> > > > > > >> 71 West 23rd Street, 8th floor
> > > > > > >> New York, NY 10010
> > > > > > >> tel: (212) 845-4495 (Tu, Th)
> > > > > > >> tel: (732) 512-0171 (M, W, F)
> > > > > > >> fax: (917) 438-0894
> > > > > > >>
> > > > > > >
> > > > > >
> > > > > > --
> > > > > > Chuck Cleland, Ph.D.
> > > > > > NDRI, Inc.
> > > > > > 71 West 23rd Street, 8th floor
> > > > > > New York, NY 10010
> > > > > > tel: (212) 845-4495 (Tu, Th)
> > > > > > tel: (732) 512-0171 (M, W, F)
> > > > > > fax: (917) 438-0894
> > > > > >
> > > > >
> > > > > ______________________________________________
> > > > > R-help at stat.math.ethz.ch 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.
> > > > >
> > > >
> > >
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>
>
>
> ------------------------------------------------------------------------------
> Notice:  This e-mail message, together with any attachment...{{dropped}}



More information about the R-help mailing list