Mars
Mars

Reputation: 8854

How to change Lattice graphics default groups colors?

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.

Upvotes: 0

Views: 3441

Answers (1)

BenBarnes
BenBarnes

Reputation: 19454

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.

Upvotes: 4

Related Questions