[R-sig-Geo] Choropleth map
James Rooney
ROONEYJ4 at tcd.ie
Wed Jun 25 14:40:17 CEST 2014
Dear Janka,
I can't answer your ggplot questions as I don't really use it much however I did spot something in your code that will cause problems.
You used a "merge" to combine data between a dataframe and an SPDF:
> ValueMapDf <- merge(nuts, Value, by.x="NUTS_ID", by.y="NUTS_ID")
I've forgotten the reasoning, but this will not give you the merge results you expect. You might want to do some manual checking on your merge results.
James
________________________________________
From: r-sig-geo-bounces at r-project.org [r-sig-geo-bounces at r-project.org] On Behalf Of Janka VANSCHOENWINKEL [janka.vanschoenwinkel at uhasselt.be]
Sent: 24 June 2014 18:35
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Choropleth map
Dear list members,
I am trying to make a basic choropleth map for all nuts3 regions in Europe
( = a map where
regions are colored by the value of a certain variable – think for instance
about a populations
density map).
I am struggling with this for over a week by now and I always get the same
error message:
Error: Aesthetics must either be length one, or the same length as
the dataProblems:ValueMapDf$NUTS_ID
I checked the book “ggplot2” from Hadley Weckham and several internet
sources. They
document the different steps very properly, but still I don’t know what I
am doing wrong.
Could somebody help me out, please? Thanks in advance!
*Below you can find my code. I hope I gave sufficient information,
otherwise, just let me know!*
library(maptools)
library(ggplot2)
library(ggmap)
# read administrative boundaries (I call the SpatialPolygonsDataFrame:
"nuts" since it contains all nuts3 regions of Europe) )
nuts <- readShapePoly(fn="NUTS_RG_60M_2006")
names(nuts)
[1] "OBJECTID" "NUTS_ID" "STAT_LEVL_" "AREA" "LEN"
"Shape_Leng" "Shape_Area"
# Then I have a dataset with over 60000 observations (several observations
per nuts3 region). I take the average of the different
observations per nuts3 region and save it as "Value"
# The dataset is called Alldata and MEt is the variable I would like to map
over the different nuts3 regions
Value <- aggregate(Alldata$MEt,list(Alldata$nuts3),mean)
colnames(Value) <- c("NUTS_ID","ValueMEt")
head(Value)
NUTS_ID ValueMEt
1 AT111 0.04856170
2 AT112 0.05448523
3 AT113 0.04563415
4 AT121 0.02164469
5 AT122 0.02664611
6 AT123 0.03163034
# merge map and data: I call the result "ValueMapDf"
ValueMapDf <- merge(nuts, Value, by.x="NUTS_ID", by.y="NUTS_ID")
ValueMapDf <- ValueMapDf[order(ValueMapDf$NUTS_ID),]
# then I dropped some regions of the SpatialPolygonsDataFrame, for which I
don't have observations (I did this manually since I didn't
know if there was a way to do it automatically)
ValueMapDf<-ValueMapDf[ValueMapDf$OBJECTID > 441,]
ValueMapDf<-ValueMapDf[ValueMapDf$OBJECTID != 444,]
ValueMapDf<-ValueMapDf[ValueMapDf$OBJECTID != 447,]
….
ValueMapDf<-ValueMapDf[ValueMapDf$OBJECTID != 1927,]
# So far so good: now I continue with plotting my map with the values!
However, as you can see below, here something goes wrong.
# ggplot mapping
# data layer
m0 <- ggplot(data=ValueMapDf)
# fill with Value data
m1 <- m0 + geom_polygon(aes(x=long, y=lat, group=ValueMapDf$NUTS_ID,
fill="ValueMapDf$ValueMEt")) + coord_equal()
# add map with only borders (to have visible borders)
m2 <- m1 + geom_path(aes(x=long, y=lat, group=ValueMapDf$NUTS_ID),
color='black')
m2
Error: Aesthetics must either be length one, or the same length as the
dataProblems:ValueMapDf$NUTS_ID
I also tried:
qplot(long,lat,data=ValueMapDf, group=ValueMapDf$NUTS_ID,
fill=ValueMapDf$ValueMEt, geom="polygon")
Regions defined for each Polygons
Error: Aesthetics must either be length one, or the same length as the
dataProblems:ValueMapDf$ValueMEt, ValueMapDf$NUTS_ID
> length(ValueMapDf$OBJECTID)
[1] 1122
> length(ValueMapDf)
[1] 1122
> length(ValueMapDf$NUTS_ID)
[1] 1122
> length(ValueMapDf$ValueMEt)
[1] 1122
*When I write:*
m0 <- ggplot(data=ValueMapDf)
# fill with Value data
m1 <- m0 + geom_polygon(aes(x=long, y=lat, group=NUTS_ID,
fill="ValueMapDf$ValueMEt")) + coord_equal()
# add map with only borders (to have visible borders)
m2 <- m1 + geom_path(aes(x=long, y=lat, group=NUTS_ID), color='black')
m2
It get: Error in eval(expr, envir, enclos) : object 'NUTS_ID' not found
Thanks in advance!
Janka
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list