Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to incorporate updated line colours into legend of a plot in R using lattice?

Tags:

r

lattice

Context and Question:

I want to add a legend to a lattice plot in R that shows the density of two groups. I've changed the default colours to black and gray. However, the legend has not updated the colours.

  • How can I get the lattice plot to use my updated colours in the legend?
  • How can I control the position of the legend (I'd like to be able to place it inside the plot area)?

Working example:

set.seed(4444)
x1 <- rep("Group A", 50)
x2 <- rep("Group B", 50)
y1 <- rnorm(50, 0, 2)
y2 <- rnorm(50, 1, 2)
dtf <- data.frame(x=c(x1, x2), y =c(y1, y2))

print(densityplot(~y, groups=x, data=dtf,
    pch=".",
    cex=2,
    col=c("black", "gray"),
    auto.key=TRUE,
    xlab="Y"))

enter image description here

like image 908
Jeromy Anglim Avatar asked Dec 13 '11 05:12

Jeromy Anglim


1 Answers

The legend color is a well-known annoyance in lattice. It looks like it is difficult to correct, because Deepayan recommends simpleTheme as a solution. For positioning, see Josh's answer.

print(densityplot(~y, groups=x, data=dtf,
              pch=".",
              cex=2,
              par.settings=simpleTheme(col=c("gray","black")),
              auto.key=TRUE,
              xlab="Y"))
like image 107
Dieter Menne Avatar answered Oct 19 '22 23:10

Dieter Menne