Advertisement
Guest User

Example choropleth map in R

a guest
Feb 17th, 2013
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.08 KB | None | 0 0
  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"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement