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") -------------- next part -------------- A non-text attachment was scrubbed... Name: Rplot01.png Type: image/png Size: 5072 bytes Desc: Rplot01.png URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140112/39ab58da/attachment-0002.png>
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
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