Context and Question:
I want to add a legend to the lattice graph in R, which shows the density of two groups. I changed the default colors to black and gray. However, the legend did not update the colors.
- How can I make the grid graph use my updated colors in a legend?
- How can I control the position of the legend (I would like to place it inside the chart 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"))

source share