Search code examples
rmapsraster

Colour polygons of a map in R?


I have used Raster package to upload the map of Spain by provinces (level 2) and I'd like to fill them with colours according to theincome per capita. Here there's the file with the income per share and the number assigned to each province. Notice that ID_2 is the number assigned by the package Raster, and the variable PROV is the official number assigned by the Spanish Government.

library(raster)
esp<-getData('GADM', country="ESP", level=2)
espPols <- unionSpatialPolygons(esp, esp$ID_2)
renta <- read.table("renta.csv",sep = ";", header=TRUE)
espMapRenta <- SpatialPolygonsDataFrame(espPols, renta)
plot(espMapRenta)

The first problem I encounter is that there are some provinces repeated in the package, and the second one is that I don't know how to fill each province in a gradient colour by the level of income.

Thank you very much for your help!! PS. The link to the income per capita data is here: https://www.dropbox.com/s/si6zpv7p2nap9zg/renta.csv?dl=0


Solution

  • Are you looking for something like that?

    library(sp)
    spplot(espMapRenta, zcol="Renta.per.Capita")
    

    enter image description here