2

I have plotted some train data with the following command

obj<-featurePlot(x=otto.all[,1:5], y = otto.all$target, plot="pairs", auto.key = list(columns = 9))

data contains 9 classes in target. Unfortunately, I found, that function had repeated colors for last two classes:

enter image description here

UPDATE

The following code

obj<-featurePlot(x=otto.all[,1:5], y = otto.all$target, plot="pairs", auto.key = list(columns = 9),col=c("#d82b25","#073ca5","#9c9ea0","#ffce49","#03f92c","#16f4c2","#69167e","#191a92","#000000"))

apparently affects individual plots, but not the legend:

enter image description here

As you see, cyan presents in plot, but absent in legend. Also legent still repeats last two colors.

(last two colors are blue and magenta as first two)

How to make it either not repeat automatically, or assign specific colors explicitly?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

3 Answers3

1

Changing the superpose.symbol graphical parameter does the trick.
To see your current theme use show.settings(); to see the current values for superpose.symbol use str(trellis.par.get("superpose.symbol")). These are the values we need to change.

So, lets come up with a set of colors, say

myColors<- c("#000000", "#7fff00", "#8b0000", "#9932cc", "#ff7f00", "#458b00", "#008b8b", "#0000ff", "#ffff00" )

and nine different symbols too, for good measure,
pch_vector <- c(0,6,12,18,1, 8, 19, 15, 17).

Next, create the new settings:

 my_settings <- list(superpose.symbol=list(alpha = rep(1, 9), col=myColors,
    cex=rep(0.8, 9), fill= myColors, font = rep(1, 9), pch=pch_vector)

Then you can change the settings globally by using:

trellis.par.set(my_settings)

or locally, by using the par.settings= argument within your featurePlot() call:

    obj<-featurePlot(x=otto.all[,1:5], y = otto.all$target, plot="pairs", 
auto.key = list(columns = 9), par.settings=my_settings) 
lilbludot
  • 71
  • 9
0

The function featurePlot uses lattice to create the plot. Try to add ,col=c("blue","green",...) as an argument and add the 9 colours that you want. You can also use hex colour codes. For instance:

,col=c("#d82b25","#073ca5","#9c9ea0","#ffce49","#03f92c","#16f4c2","#69167e","#191a92","#000000")

Jonas Tundo
  • 6,137
  • 2
  • 35
  • 45
0

If you use a theme then auto.key is affected. For example you should see a difference in:

featurePlot(iris[, 1:4], iris$Species, plot="pairs", 
            auto.key = list(columns = 2))

and

col.whitebg()
featurePlot(iris[, 1:4], iris$Species, plot="pairs", 
            auto.key = list(columns = 2))
topepo
  • 13,534
  • 3
  • 39
  • 52