[R] color coding a legend

Robert Schick Robert.Schick at noaa.gov
Tue Jun 10 21:13:57 CEST 2003


I'm using R 1.6.2 on a Windows 2000 machine.

I've plotted the results of an MDS run labeled by a numerical ID, and
color coded by a group code:

plot(cv.mds.spr$points, type="n", main="Non-Metric Multidimensional
Scaling of SprRun CV Watersheds")
text(cv.mds.spr$points, labels = as.character(cv.wshed.id.spr), col =
codes(cv.wshed.grp), cex=.75)

Question is, how do I get the legend to match the color codes?

I have tried different permutations of the following: 
leg.txt <- c("LSSJ.NS","LSSJ.SS","US.RD","US.SF")   # the groups in
cv.wshed.grp
legend(-6.5, -2.5, leg.txt, pch="1234", col=
as.character(codes(cv.wshed.grp)))

But this only plots the codes in cv.wshed.grp as they are encountered,
not the levels. Ideally I'd like the legend to have the Group ID label,
and a filled box corresponding to the colors in the text call above.


-- 
Rob Schick
Ecologist
NOAA Fisheries
Santa Cruz Lab
110 Shaffer Road
Santa Cruz, CA 95060
Phone: 831.420.3960




More information about the R-help mailing list