[R] adding legend to choropleth map using GISTools

Jim Lemon jim at bitwrit.com.au
Mon Jan 13 05:53:10 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 again Martha,
As I don't have the GIStools package, I missed the obvious. I think you 
have specified the legend position outside the plot area. Try:

par("usr")

to find out the limits of the plot in user units.

Jim




More information about the R-help mailing list