I have a question with regard to plotting a choropleth map. What is recommended with regard to plotting a map with custom breaks? And make it look a bit attractive.
In my script I have used the plot and spplot command to visualize some data but I am not entirely happy with the result. It is easy to see why. Using spplot I get a colour ramp that repeats colours for different values which is a bit weird. Using plot with the custom breaks I have not been able to make better intervals for the values, such that for instance zero-values are white on the map. The skewed proportions between legend and map are I think probably the result of Rstudio.
So I wondered whether someone has better suggestions for plotting choropleth maps in R using custom breaks?
EDIT 20-02-2013
As suggested I have used ggplot2 for creating a choropleth map. I've used the code below. I encountered two problems. One is that after fortifying the data not all data from the shapefile is transferred to the data frame. So I have to rename the "id" variable such that I can merge the data. Minor issue.
A bigger issue is that despite the fact that the data in the dataframe is correct, the ggplot shows a map where the data is incorrect. The colour value according to the legend does not correspond with the value of the variable in the data. Am I overlooking something here?
# Plotting polygon shapefiles (try-out)
# Load the packages
require(rgdal)
require(maptools)
require(ggplot2)
gpclibPermit() # required for the fortify method
# Load the shapefile
africa=readOGR("/home/GIS",layer="africaII")
names(africa)
# Load the data on conflict
conflict<-read.csv("africa_conflict.csv", header=TRUE)
names(conflict)
# Merge the data together
africadat<-merge(africa, conflict, by="ISO3")
# Changing the data in the shapefile
africa@data <- africadat
# Fortify so that ggplot can plot
africa.points = fortify(africa,region="ISO3")
# Change "id" to "ISO3"such that the datasets can be merged again
names(africa.points)[names(africa.points)=="id"]<-"ISO3"
africa.df=merge(africa.points,africadat,by="ISO3")
# Plot the data
ggplot(africa.df) +
aes(long,lat,group=group) +
geom_polygon(aes(fill =onset))+
geom_path(color="white") +
coord_equal()
Update 13-11-2015: Fixed link to shape file. See answer below for solution on how to plot the choropleth.
ggplot2
for choropleth maps, which I think turns out better looking output by default.