[R] adding legend to choropleth map using GISTools

Jim Lemon jim at bitwrit.com.au
Mon Jan 13 03:42:31 CET 2014


On 01/13/2014 10:19 AM, Martha Zorn wrote:
> I am having problems with adding legend to choropleth map using GISTools.  The map that I get does not include a legend (see attached).   Thanks for any help you can provide.
>
> Below is my code:
> #read in sample
> at001<-read.csv("C:/Users/martha/Dropbox/shiny/sample.csv")
> a<- matrix(at001$npovr, ncol=1)
> rownames(a)<- as.character(at001$County)
> ##Sets working libraries for following code
> library(GISTools)
> library(classInt)
>
> ##Reads in MA County shape file
> #at work MAcounties<-readShapeSpatial("J:/Projects/Gambling/web/shiny/co25_d00_shp/co25_d00.shp")
> MAcounties<-readShapeSpatial("C:/Users/martha/Dropbox/shiny/co25_d00_shp/co25_d00.shp")
> sorted.a<- a[MAcounties at data[,"NAME"], 1]
>
> shades<- auto.shading(sorted.a, n = 6, cutter = rangeCuts, cols = brewer.pal(6, "Greens"))
> # Draw a map based on this scheme
> choropleth(MAcounties,sorted.a,shades)
> # Add a legend based on this scheme
> choro.legend(-82, 34.87, shades, fmt = "%4.1f", title = "Poverty Rates")
> # Add a title to the map
> title("Poverty Rates in Massachusetts by County")
> # ... and a little more explanatory text
> text(-85.3, 30.4, "Source: XXXXXX")
> # and draw a box around it
> box(which = "outer")
>
Hi Martha,
Try something like this:

library(plotrix)
color.legend(-78,42,-77.5,43,
  c("First","Second","Third","Fourth","Fifth"),
  color.scale(1:5,c(1,0),1,c(1,0)),
  gradient="y")

As I don't have your data, I can't get the colors and labels right.

Jim




More information about the R-help mailing list