gpt4 book ai didi

r - 如何在 R 中将 SpatialPointsDataFrame 更改为 SpatialPolygonsDataFrame 以在 ggplot2 中使用它?

转载 作者:行者123 更新时间:2023-12-04 11:34:31 24 4
gpt4 key购买 nike

我使用 R 使用 ggplot2 形式的大圆圈在世界地图上投影一些数据流。
我还想在我的 map 上投影城市区域:
http://www.naturalearthdata.com/downloads/

然而,这些在 SpatialPointsDataFrame 中。也许我的问题是微不足道的,但我不知道如何将文件更改为 SpatialPolygons。

我的代码如下:

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

urbanareas<-ggplot2:::fortify(simp)

我也试过:
urbanareas<-fortify.SpatialPolygonsDataFrame(simp)

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

但它们都不起作用。我必须错过一些东西......
我是 R 的初学者,如果有任何建议,我将不胜感激。

先感谢您!

PS。查找以下数据信息:
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"

最佳答案

ggplot不适用于形状文件或光栅图像等空间对象。您需要将它们转换为 data.frames。

如果您正在使用 SpatialPointsDataFrame ,这很简单:

mapdata <- data.frame(yourshapefile)

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

如果您正在使用 SpatialPolygonsDataFrame , 你需要 fortify对象,像这样:
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")

这个答案来自 ZevRoss webpage ,使用 R 进行空间分析的非常有用的来源.

关于r - 如何在 R 中将 SpatialPointsDataFrame 更改为 SpatialPolygonsDataFrame 以在 ggplot2 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20320377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com