[R] Labelling curves on graphs

Frank E Harrell Jr f.harrell at vanderbilt.edu
Thu Jul 17 13:30:34 CEST 2008

Barry Rowlingson wrote:
>     Why use color when you can use black and label the curves where they
>     are most separated?  This solves problems with color blindness,
>     xeroxing, and faxing.
> Where should I put the labels in this example:
>  > set.seed(123)
>  > z=matrix(runif(6*50),50,6)
>  > matplot(z,type='l',col=1,lty=1)
> A label is only going to indicate which curve is which at a point. If 
> the curves approach or cross anywhere you're going to get confused. Line 
> styles and colours persist along the length of the line.
> For nice smooth well-behaved curves then perhaps labelling is okay - 
> contour lines rarely cross, and even when they do they must have the 
> same height value anyway.
> I'm not sure once you get to six lines on a graph you can find six 
> distinctive line styles without using colour, so it might be better to 
> switch graphic completely. Compare the above plot with image(z).
> If the plots don't cross, one solution is to extend the plot to the 
> right and just add labels there:
>  > set.seed(123)
>  > z=matrix(runif(6*50),50,6)
>  > m=apply(z,1,cumsum)
>  > ms=apply(m,1,sort)
>  > matplot(ms,type='l',xlim=c(0,55),lty=1,col=1)
>  > text(50,ms[50,],c("Foo","Bar","Baz","Qux","Quux","Quuux"),adj=-0.1)
> I can't see a neater way of putting labels actually on the lines in this 
> case - they'll be all over the place to squeeze them in the gaps.  
> Barry

Barry - you're right; when two lines cross more than 2 or 3 times the 
suggested approach doesn't work well.  When it does work, not having the 
user have to look at a legend is a benefit.

Depending on the number of lines, using thick gray scale and thin black 
lines can be very effective.


Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

More information about the R-help mailing list