UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             
Loading

Frequently Asked Questions about S-Plus
How do I change the colors in a trellis graph?

If the default color scheme for the lines and the symbol are not to your liking it is possible to change them using the following code. We are using the willet data from Singer's article in this example.

We have a great trellis graph of the data in the Willet data from the Singer article. Unfortunately, the color of the lines and the symbols used in the graph are very hard to see and we would really like to change them.

#grouping the data so that we can graph it 
willettg <- groupedData( y ~ time | id, data=willett)
#plotting using default color scheme
plot(willettg)

The parameter specifications for the line in the trellis plot are contained in the object plot.line. We access these parameters by using the trellis.par.get function and store the parameter specifications of plot.line in the object line
Note: The name line was chosen arbitrarily.

line <- trellis.par.get("plot.line")

Then we display the current parameters for plot.line by looking at the contents of the object line.

line

> line
$col:
[1] 2

$lty:
[1] 1

$lwd:
[1] 1

We see that the object line contains the parameter specifications for the color (line$col), the line type (line$lty) and line width (line$lwd). We wish to change the color of the lines from cyanna (color=2) to dark blue (color=6) and thus we assign line$col to be 6 and use the trellis.par.set function to specify that the parameters in plot.line should use the specifications contained in the object line.
Note: To see which color correspond to which number please refer to the color wheel at the bottom of the page. The colors are numbered in a counter-clock wise order starting with the black slice which is number 1.

line$col <- 6
trellis.par.set("plot.line", line)

We verify that the line color has changed by applying the trellis.par.get function to the new (and now current) plot.line object which will list all the parameters and their specifications.

trellis.par.get("plot.line")

> trellis.par.get("plot.line")
$col:
[1] 6

$lty:
[1] 1

$lwd:
[1] 1

We now wish to make a similar change for the plotting symbol used in the trellis graph. We first use the trellis.par.get function to assign the parameters specifications contained in plot.symbol to the object new.symbol. We look at the current parameters for the plotting symbol and find that the current color parameter is cyanna (color=2) and we want to change this to dark blue (color=6). So, we assign new.symbol$col to be 6 and use the trellis.par.set function to specify that the parameters in plot.symbol should use the parameters specified in the object new.symbol. Finally, we verify that the color change has taken place.

new.symbol <- trellis.par.get("plot.symbol")
new.symbol

> new.symbol
$cex:
[1] 0.8

$col:
[1] 2

$font:
[1] 1

$pch:
[1] 1

new.symbol$col <- 6
trellis.par.set("plot.symbol", new.symbol)
trellis.par.get("plot.symbol")

> trellis.par.get("plot.symbol")
$cex:
[1] 0.8

$col:
[1] 6

$font:
[1] 1

$pch:
[1] 1

#Plotting the trellis graph of the data with the new color scheme:
plot(willettg)
pie(rep(1, 15), col=1:15)

How to cite this page

Report an error on this page or leave a comment

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California