shape$IDs , as(shape, "data.frame"), as.data.frame(shape) should all work.
On 07/03/2014 02:34 PM, James Rooney wrote: > Thank you Janka, > > I'm aware of the solution and it is what I use myself. However Roger > generally advises not to use @slots directly which is why I did not suggest > the solution. > > Roger I am curious is there an alternative here to using the @data construct > with a match function ? > > James > > ________________________________________ > From: Janka VANSCHOENWINKEL [janka.vanschoenwin...@uhasselt.be] > Sent: 03 July 2014 09:35 > To: James Rooney; roger.biv...@nhh.no > Cc: r-sig-geo@r-project.org > Subject: Re: [R-sig-Geo] Choropleth map > > Hi James and Roger, > > You both pointed out that maybe I should not use the merge function to join > data to an sp object. > > I found the explanation for this on the internet > (http://gis.stackexchange.com/) and thought it would be interesting to tell > you: > The merge function resorts the data during the operation which breaks the > internal relationship in the sp object. Therefore to merge a dataframe to the > @data slot of an sp object, using match is indeed better and is done in this > way: > > shape = name of shapefile > Otherdata = data that I want to match > IDS = identifier I want to merge on > > shape@data = data.frame(shape@data, OtherData[match(shape@data$IDS, > OtherData$IDS),]) > > > Furthermore, I have not yet entirely found the code to make the chloropleth > plot, but I am getting there! > > Thanks again for you advice, > > Janka > > > > > > 2014-06-25 14:40 GMT+02:00 James Rooney > <roone...@tcd.ie<mailto:roone...@tcd.ie>>: > 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-boun...@r-project.org<mailto:r-sig-geo-boun...@r-project.org> > [r-sig-geo-boun...@r-project.org<mailto:r-sig-geo-boun...@r-project.org>] On > Behalf Of Janka VANSCHOENWINKEL > [janka.vanschoenwin...@uhasselt.be<mailto:janka.vanschoenwin...@uhasselt.be>] > Sent: 24 June 2014 18:35 > To: r-sig-geo@r-project.org<mailto:r-sig-geo@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]] > > _______________________________________________ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Edzer Pebesma Institute for Geoinformatics (ifgi), University of Münster Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251 83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795
signature.asc
Description: OpenPGP digital signature
_______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo