How to change a SpatialPointsDataFrame into SpatialPolygonsDataFrame in R to use it after in ggplot2?

▼魔方 西西 提交于 2019-12-21 05:13:09

问题


I use R to project some data flows on a world map using great circles form ggplot2. I would like to also project on my map also urban areas from: http://www.naturalearthdata.com/downloads/

These are however in a SpatialPointsDataFrame. Perhaps my question is trivial, but I don't know how to change the file into SpatialPolygons.

My code goes as follows:

urbanareasin  <- readShapePoly("//....//ne_10m_populated_places//ne_10m_populated_places.shp") 
simp<-gSimplify(urbanareasin, tol=0.1)

urbanareas<-ggplot2:::fortify(simp)

I tried also:

urbanareas<-fortify.SpatialPolygonsDataFrame(simp)

and:

urbanareas<-ggplot2:::fortify.SpatialPolygonsDataFrame(simp)

but neither of them works. I have to be missing something... I'm a beginner in R and I would appreciate a lot any suggestions.

Thank you in advance!

PS. Find below data information:

str(urbanareasin) # to get info about the object

Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots

..@ data :'data.frame': 7322 obs. of 92 variables:

.. ..$ SCALERANK : int [1:7322] 10 10 10 10 10 10 10 10 10 10 ...

.. ..$ NATSCALE : int [1:7322] 1 1 1 1 1 1 1 1 1 1 ...

.. ..$ LABELRANK : int [1:7322] 8 8 8 8 8 8 8 7 7 7 ...

.. ..$ FEATURECLA: Factor w/ 8 levels "Admin-0 capital",..: 4 4 4 4 4 4 4 4 4 4 ...

.. ..$ NAME : Factor w/ 7069 levels "'Ataq","'s-Hertogenbosch",..: 1453 6358 2017 1135 1973 612 5894 3924 2991 6136 ...

.. ..$ NAMEPAR : Factor w/ 81 levels "Adi Ugri","Alleppey",..: NA NA NA NA NA NA NA NA NA NA ...

.. ..$ NAMEALT : Factor w/ 454 levels "\xdcr\xfcmqi|Wulumqi",..: NA NA NA NA NA NA NA NA NA NA ...

.. ..$ DIFFASCII : int [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ NAMEASCII : Factor w/ 7063 levels "'Ataq","'s-Hertogenbosch",..: 1441 6355 2008 1125 1964 605 5887 3912 2981 6127 ...

.. ..$ ADM0CAP : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ CAPALT : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ CAPIN : Factor w/ 23 levels "Administrative",..: NA NA NA NA NA NA NA NA NA NA ...

.. ..$ WORLDCITY : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MEGACITY : int [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ SOV0NAME : Factor w/ 200 levels "Afghanistan",..: 191 191 191 191 191 177 177 180 180 180 ...

.. ..$ SOV_A3 : Factor w/ 201 levels "AFG","AGO","ALB",..: 189 189 189 189 189 175 175 182 182 182 ...

.. ..$ ADM0NAME : Factor w/ 223 levels "Afghanistan",..: 214 214 214 214 214 200 200 203 203 203 ...

.. ..$ ADM0_A3 : Factor w/ 223 levels "ABW","AFG","AGO",..: 210 210 210 210 210 196 196 203 203 203 ...

.. ..$ ADM1NAME : Factor w/ 2477 levels "?li Bayramli",..: 535 718 1856 431 719 1065 460 1318 1100 2202 ...

.. ..$ ISO_A2 : Factor w/ 225 levels "-99","AD","AE",..: 212 212 212 212 212 197 197 202 202 202 ...

.. ..$ NOTE : Factor w/ 6 levels "1","From 1996 as a summer only station",..: NA NA NA NA NA NA NA NA NA NA ...

.. ..$ LATITUDE : num [1:7322] -34.5 -33.5 -33.1 -34.5 -34.1 ...

.. ..$ LONGITUDE : num [1:7322] -57.8 -56.9 -58.3 -56.3 -56.2 ...

.. ..$ CHANGED : num [1:7322] 4 4 4 4 4 4 4 4 4 4 ...

.. ..$ NAMEDIFF : int [1:7322] 1 1 1 1 1 1 1 1 1 1 ...

.. ..$ DIFFNOTE : Factor w/ 52 levels "Added","Added from GeoNames for UN mega cities.",..: 9 9 9 9 9 9 9 6 9 9 ...

.. ..$ POP_MAX : int [1:7322] 21714 21093 23279 19698 32234 61845 21054 61705 19875 62577 ...

.. ..$ POP_MIN : int [1:7322] 21714 21093 23279 19698 32234 61845 21054 61705 19875 62577 ...

.. ..$ POP_OTHER : int [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ RANK_MAX : int [1:7322] 7 7 7 6 7 8 7 8 6 8 ...

.. ..$ RANK_MIN : int [1:7322] 7 7 7 6 7 8 7 8 6 8 ...

.. ..$ GEONAMEID : num [1:7322] 3443013 3439749 3442568 3443413 3442585 ...

.. ..$ MEGANAME : Factor w/ 588 levels "\xdcr\xfcmqi (Wulumqi)",..: NA NA NA NA NA NA NA NA NA NA ...

.. ..$ LS_NAME : Factor w/ 6559 levels "25 de Mayo","28 de Noviembre",..: NA NA NA NA NA NA NA NA NA NA ...

.. ..$ LS_MATCH : int [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ CHECKME : int [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_POP10 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_POP20 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_POP50 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_POP300: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_POP310: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_NATSCA: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_AREAKM: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_AREAKM: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_AREAMI: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_AREAMI: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_PERKM : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_PERKM : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_PERMI : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_PERMI : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_BBXMIN: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_BBXMIN: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_BBXMAX: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_BBXMAX: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_BBYMIN: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_BBYMIN: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MIN_BBYMAX: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MAX_BBYMAX: num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MEAN_BBXC : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ MEAN_BBYC : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ COMPARE : int [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ GN_ASCII : Factor w/ 5960 levels "'Ayoun el 'Atrous",..: 1236 5382 1692 979 1656 529 5000 NA 2536 5197 ...

.. ..$ FEATURE_CL: Factor w/ 1 level "P": 1 1 1 1 1 1 1 NA 1 1 ...

.. ..$ FEATURE_CO: Factor w/ 10 levels "PPL","PPLA","PPLA2",..: 1 1 1 1 1 1 1 NA 1 1 ...

.. ..$ ADMIN1_COD: num [1:7322] 4 6 12 2 7 5 22 0 31 34 ...

.. ..$ GN_POP : num [1:7322] 21714 21093 23279 19698 32234 ...

.. ..$ ELEVATION : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ GTOPO30 : num [1:7322] 28 134 43 29 74 463 374 0 49 247 ...

.. ..$ TIMEZONE : Factor w/ 319 levels "Africa/Abidjan",..: 121 121 121 121 121 32 32 NA 49 49 ...

.. ..$ GEONAMESNO: Factor w/ 16 levels "Added from GeoNames.",..: 2 2 2 2 2 5 2 13 2 5 ...

.. ..$ UN_FID : int [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ UN_ADM0 : Factor w/ 119 levels "Afghanistan",..: NA NA NA NA NA NA NA NA NA NA ...

.. ..$ UN_LAT : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ UN_LONG : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1950 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1955 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1960 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1965 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1970 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1975 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1980 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1985 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1990 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP1995 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP2000 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP2005 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP2010 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP2015 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP2020 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP2025 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ POP2050 : num [1:7322] 0 0 0 0 0 0 0 0 0 0 ...

.. ..$ CITYALT : Factor w/ 140 levels "Ad Damman","Al Hudaydah",..: NA NA NA NA NA NA NA NA NA NA ...

..@ coords.nrs : num(0)

..@ coords : num [1:7322, 1:2] -57.8 -56.9 -58.3 -56.3 -56.2 ...

.. ..- attr(*, "dimnames")=List of 2

.. .. ..$ : NULL

.. .. ..$ : chr [1:2] "coords.x1" "coords.x2"

..@ bbox : num [1:2, 1:2] -179.6 -90 179.4 82.5

.. ..- attr(*, "dimnames")=List of 2

.. .. ..$ : chr [1:2] "coords.x1" "coords.x2"

.. .. ..$ : chr [1:2] "min" "max"

..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots

.. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

回答1:


ggplot does not work with spatial objects like shape files or raster images. You need to convert them into data.frames.

In case you're working with SpatialPointsDataFrame, this is as simple as:

mapdata <- data.frame(yourshapefile)

# now create the map
  ggplot() +  geom_point( data= mapdata, aes(x=long, y=lat), color="red")

In case you're working with SpatialPolygonsDataFrame, you need to fortify the object, like this:

yourshapefile_df <- fortify(yourshapefile, region ="id")

# now create the map
  ggplot() + geom_point(data= yourshapefile_df, aes(x=long, y=lat, group=group), color="red")

This answer comes from ZevRoss webpage, a very useful source for spatial analysis using R.



来源:https://stackoverflow.com/questions/20320377/how-to-change-a-spatialpointsdataframe-into-spatialpolygonsdataframe-in-r-to-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!