I'm trying to do a very basic map plot with ggplot2. I can't find why the colored polygons would not display. It seems that my code is no different from what I can find on the many tutorials and the questions already answered on this website. I think it could come from the way I prepare the data (see 100% reproducible example below).
library(maptools)
library(sp)
library(ggplot2)
library(rgeos)
con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/GHA_adm1.RData")
print(load(con))
close(con)
ghaDF<-as.data.frame(gadm)
ghaDF$prod <- c(12, 26, 12,22,0,11,4,5,4,4) # add values for the regions to be colored with
gadm@data$id = rownames(gadm@data) #create an id in the shapefile data
ghaMap <- fortify(gadm, region="id")
colnames(ghaDF[5])<-"id"
ghaMap <- merge(ghaMap, ghaDF)
m0 <- ggplot(data=ghaMap)
m1 <- m0 + geom_polygon(aes(x=long, y=lat, fill = prod, group=group))
+ scale_fill_gradient(low = "light green", high = "dark green")
m2 <- m1 + geom_path(aes(x=long, y=lat, group=group),color='gray')
+ coord_equal()
m2
On the image above (the output of m2
), the regions should be colored according to the ghaMap$prod
variable. Any suggestions?
(R version 3.0.2 - Platform: x86_64-w64-mingw32/x64 (64-bit))