
Example choropleth map in R
By: a guest on
Feb 17th, 2013 | syntax:
R | size: 1.08 KB | hits: 34 | expires: Never
# Choropleth map in R (try-out)
# Load required libraries
library(sp)
library(maptools)
library(RColorBrewer)
library(classInt)
# Read in the shapefile
africa<-readShapeSpatial("/home/GIS/africaII.shp")
names(africa)
# Read in the data
conflict<-read.csv("africa_conflict.csv", header=TRUE)
# Add column with row order
africa@data <- cbind(c(1:dim(africa)[1]),africa@data[,c("ISO3","NAME","LAT","LON")])
names(africa@data) <- c("order",names(africa@data)[-1])
# Merge the data into a new dataframe
africadat<-merge(africa, conflict, by="ISO3")
# Replace original attribute table
africa@data <- africadat[order(africadat$order),]
# Set colours for the breaks
colours <- brewer.pal(9, "Blues")
# Create the breaks
brks<-classIntervals(africa$onset,n=5,style="pretty")
brks<-brks$brks
# Plot the map
# Using the normal plot function
plot(africa, col=colours[findInterval(africa$onset,brks,all.inside=TRUE)])
# Add a lagend
legend(x="bottomleft",legend=leglabs(brks), fill=colours, bty="n")
# Using the spplot function
spplot(africa,"onset",col.regions=brewer.pal(9,"Blues"))