Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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()

Link to replication data

share|improve this question
4  
I would also look at using ggplot2 for choropleth maps, which I think turns out better looking output by default. –  SlowLearner Feb 17 '13 at 17:02
    
Yeah I wasn't sure about using ggplot2 since the long/lat data in my shapefile only specifies a point and not the outline of the national borders. So therefore I used these plots in order to create a choropleth map. I'm not very familiar with handling GIS data in R so not sure if I'm overlooking some really straightforward solution for that. –  BlankUsername Feb 17 '13 at 20:38
add comment

2 Answers 2

The classInt package has a number of functions for computing intervals for numeric data - install it and read the docs.

share|improve this answer
    
Yes, I used the classInt package with fixed breaks (not shown in the script) but the issue with that is that it computes intervals for continuous numerical data. Whereas my intervals are discrete, they represent a frequency of events. Haven't found out yet how to improve upon that. –  BlankUsername Feb 17 '13 at 20:34
add comment

I don't see a problem with the above code except that you have 6 clear levels in onset, so I would treat them as factors rather than as a continuous variable. Note the factor call in the code below.

ggplot(africa.df) +
  aes(long,lat,group=group) +
  geom_polygon(aes(fill = factor(onset)))+
  geom_path(color="white") +
  coord_equal()

That code gives the following result:

original

If you want custom colours you could try this:

library(RColorBrewer)
my.cols <- brewer.pal(6, "Blues")

ggplot(africa.df) +
  aes(long,lat,group=group) +
  geom_polygon(aes(fill = factor(onset)))+
  scale_fill_manual("Onset\n", values = my.cols, guide = "legend") +
  geom_path(color="white") +
  coord_equal()

Which produces this:

custom colours

share|improve this answer
    
It still plots the wrong values though. Very peculiar. –  BlankUsername Feb 20 '13 at 20:04
    
OK, can you specify exactly what the issue is, with examples? Angola is assigned a 3 for the onset column and plots a 3 in the chart. That seems to work. Presumably I'm missing something? –  SlowLearner Feb 20 '13 at 20:30
    
Ow yes sorry. Angola is one of the few that is correct. If you look at South Africa, Namibia, or Senegal for instance you'll see that the value as indicated by the colour does not correspond with the value in the data. So Senegal should be a 6(now a 2), South Africa 0 (4) and Namibia also 0 (1). Burundi and Rwanda are also off to name a few other. In the link a plot I made with spplot which shows a map with the correct values. This plot is done directly from the shapefile. So I guess something goes wrong with creating the dataframe. –  BlankUsername Feb 20 '13 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.