[R] Colors and legend on a scatterplot?

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri Jan 14 18:02:44 CET 2005


buczkowski at delta.sggw.waw.pl wrote:

> Hello
> 
> I am new to R and I cannot overcome the 2 problems
> 
> 1.Defining my own color scale for a value on a scatterplot
> 2.Adding legend showing values for colours
> 
> I have a set of points in area of my research with coordinates X and Y and
> a value which I would like to show with colors.
>  
> #values for points are residuals from regression
> 
> stepmod = step(wmodzad, direction="both")
> 
> #I am adding residuals to original data to get coordinates for residuals
> 
> expzad = cbind(zadrz, residuals(stepmod))
> 
> #I want to have colors for classes defined as standard deviation from mean
> 
> 
> std = sd(residuals(stepmod))
> mn = mean(residuals(stepmod))
> sdclas = seq(-3*std+mn, 3*std+mn, by=std)
> 
> #I would like to have colors from green to red, but I can't do it so I am
> doing only
> 
> palette( rainbow(6) )

You could either use something like the RColorBrewer package, or specify 
the colors yourself using, e.g., rgb().



> #I am dividing residuals into earlier defined classes by
> 
> residsd = cut(residuals(stepmod), sdclas, labels=FALSE) 
> 
> #or by (THEN POINTS DON'T APPEAR ON PLOT IN NEXT STEP)
> 
> residsd = cut(residuals(stepmod), sdclas)
> 
> # I am creating a plot
> 
> plot(expzad$X,zadrz$Y,col=residsd, pch=15 )
> 
> # I would like to add legend to plot showing values for colours from plot
> but I can't do this. Simply help for 'legend' on my level of knowledge on
> R is not helpfull.
> 
> loc = locator(1)
> legend(loc$x, loc$y, legend = levels(residsd) )

I guess you want something like

legend(loc$x, loc$y, col = residsd, legend=levels(residsd), pch=15)

Uwe Ligges

> Thanks in advance for your help
> 
> Rafal Buczkowski
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list