Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Example choropleth map in R

By: a guest on Feb 17th, 2013  |  syntax: R  |  size: 1.08 KB  |  views: 72  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Choropleth map in R (try-out)
  2. # Load required libraries
  3. library(sp)
  4. library(maptools)
  5. library(RColorBrewer)
  6. library(classInt)
  7. # Read in the shapefile
  8. africa<-readShapeSpatial("/home/GIS/africaII.shp")
  9. names(africa)
  10. # Read in the data
  11. conflict<-read.csv("africa_conflict.csv", header=TRUE)
  12. # Add column with row order
  13. africa@data <- cbind(c(1:dim(africa)[1]),africa@data[,c("ISO3","NAME","LAT","LON")])
  14. names(africa@data) <- c("order",names(africa@data)[-1])
  15. # Merge the data into a new dataframe
  16. africadat<-merge(africa, conflict, by="ISO3")
  17. # Replace original attribute table
  18. africa@data <- africadat[order(africadat$order),]
  19. # Set colours for the breaks
  20. colours <- brewer.pal(9, "Blues")
  21. # Create the breaks
  22. brks<-classIntervals(africa$onset,n=5,style="pretty")
  23. brks<-brks$brks
  24. # Plot the map
  25. # Using the normal plot function
  26. plot(africa, col=colours[findInterval(africa$onset,brks,all.inside=TRUE)])
  27. # Add a lagend
  28. legend(x="bottomleft",legend=leglabs(brks), fill=colours, bty="n")
  29. # Using the spplot function
  30. spplot(africa,"onset",col.regions=brewer.pal(9,"Blues"))
clone this paste RAW Paste Data