[R] colors in R [was "Re: legend() buglet 2"]

Martin Maechler maechler at stat.math.ethz.ch
Wed Dec 16 14:22:56 CET 1998

	[CC: to "R-help", since this should be of wider interest ]

>>>>> "PaulG" == Paul Gilbert <pgilbert at bank-banque-canada.ca> writes:

    PaulG> Perhaps this is not a bug but rather my misunderstanding of lty
    PaulG> and col. I would like to generate a legend for plots as follows:

No, there's definitely no bug here.

    PaulG> par(mfcol = c(2, 1), mar = c(5.1, 6.1, 4.1,  2.1))
    PaulG> matplot(t(matrix(1:8,8,20)), type="l")
    PaulG> par(mfg = c(2, 1, 2, 1))
    PaulG> box(col = 0) #  Rbug workaround. Thanks Martin
    PaulG> legend((par()$usr)[1:2], (par()$usr)[3:4], as.character(1:8),
    PaulG> 	lty=1:8, col=1:8, bty="y")

    PaulG> The lines for 7 and 8 do not appear in the legend.

No!  They do, but you don't see them easily:
> palette()
[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" 
[8] "white"  

I.e., col=7 is "yellow"  (per default, you can always change this!)
      col=8 is "white"
both of which are hard to see with a white background.

Try to say first

	par(bg = "gray80") # or something else between white and black

and you will see the  "yellow" and "white".

    PaulG> matplot(t(matrix(1:8,8,20)), type="l", lty=1:8, col=1:8)

    PaulG> they do not appear in the graph.

as above, they do, look at it after   par(bg = "gray")# or "gray80"

For further ``color manipulation'' in R,
please look at the help page for palette and its examples and the further
pages in 'see also'.

As another illustrative example for matplot(), try e.g.,

par(bg="light blue")
matplot(t(matrix(1:20,20, 2)), type="l", lty=1:4, col=1:20, lwd=2)


The other ``phenomenon'' you observe really comes from  
matplot()'s default of  "col = 1:6" (and then recycling).
It makes sense to only use ``a few'' different colors in a general matplot
(where lines cross,...).
S default even recycles only four colors  (col=1:4).

Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum SOL G1;	Sonneggstr.33
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1086			<><
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list