[R] Help in plotting a legend

Jim Lemon jim at bitwrit.com.au
Wed Aug 26 13:28:43 CEST 2009


Ashutosh Nandeshwar wrote:
> Hello, List,
>
>  
>
> I am a new user of the R project, and I need some help in plotting a legend.
> I am using the PBSmapping library to plot map of Ohio and heat color it with
> the count of employees in each county. As a guide, I am using "Data Mashups
> in R." I am able to plot the map with the colors; however, I would like to
> put a legend with a single box full of these colors, but only show the max
> and the min value on the left corner and the right corner respectively. 
>
>  
>
> Here's a  part of the code I am using:
>
> #empdata has the employee data with the latitude and longitude for each
> employee
>
> library(PBSmapping)
>
> addressEvents<-as.EventData(empdataRC,projection=NA)
>
> #myShapeFile is a shape file of Ohio imported using importShapefile
>
> addressPolys<-findPolys(addressEvents,myShapeFile) 
>
>  
>
> myTrtFC<-
> table(factor(addressPolys$PID,levels=levels(as.factor(myShapeFile$PID)))) 
>
> log(myTrtFC)->lTrt
>
> mapColors<-heat.colors(max(lTrt)+1,alpha=.6)[max(lTrt)-lTrt+1] 
>
> mapColors[ is.na(mapColors) ] <- "white"
>
> pdf("RC-Employees.pdf",version="1.4")
>
> plotPolys(myShapeFile,axes=FALSE,bg="white",main=" Employees
> ",xlab="",ylab="",col=mapColors) 
>
> #here is the current legend, but as you can see, the colors and the box
> count doesn't match
>
> #i would like to see only one box with the gradients of the colors that I
> used, and the max and the min value on top. I even tried a rectangle but I
> could not plot it
>   
Hi Ashutosh,
Have a look at color.legend in the plotrix package.

Jim




More information about the R-help mailing list