0

When using groups, Lattice gives each group a different color. Example:

df <- data.frame(x=1:56, y=rnorm(56), class=1:14) # create some data
xyplot(y ~ x, groups=class, data=df, type="l", auto.key=list(space="right"))

However, by default Lattice only uses seven colors, as running the example above will show. If you have more than seven groups, Lattice cycles through the colors again in order, causing data from distinct groups to have the same color. I learned from another Stackoverflow article that these colors are stored in trellis.par.get()$superpose.symbol$col. I want to make the list of groups colors longer (without having to specify colors explicitly in plotting calls). I can't figure out how to change this list of colors, however. (This might be due to ignorance about some basic facts about Lattice syntax or semantics.) This illustrates the problem:

> trellis.par.get()$superpose.symbol$col
[1] "#0080ff"   "#ff00ff"   "darkgreen" "#ff0000"   "orange"    "#00ff00"   "brown"    
> class(trellis.par.get()$superpose.symbol$col)
[1] "character"
> mycolors <- c(trellis.par.get()$superpose.symbol$col, "navyblue", "purple", "gold")
> trellis.par.get()$superpose.symbol$col[1:10] <- mycolors
Error in trellis.par.get()$superpose.symbol$col[1:10] <- mycolors : 
  invalid (NULL) left side of assignment

I don't understand what that error message is telling me.

Mars
  • 8,689
  • 2
  • 42
  • 70

1 Answers1

4

You should be using trellis.par.set() to set trellis graphical parameters. So:

trellis.par.set(superpose.symbol = list(col = mycolors))

Bear in mind that this will only change the settings for the currently active device, so that if you create a new graphical device, you will have to reset the color settings.

Also, this is explained in the help page ?trellis.par.get in the Details section. Please have a look there.

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
  • I had just realized that I'd foolishly ignored the fact that there was a function called ...set(). However, I'm glad that I asked, because your way of setting the parameter is much simpler than the way that I just figured out. I had glanced at the help page, but even looking at the Details section now after understanding your simple explanation, I find it difficult to follow without thorough study. Thank you! – Mars Oct 19 '12 at 05:43
  • @Mars, yeah, I find lattice a great package, and I also find that I spend a lot of time trying to understand and use its intricacies! – BenBarnes Oct 19 '12 at 05:47