如何在fortify()之后保留shapefile中的信息

Rco*_*ter 14 r ggplot2 rgdal

如何在shapefile之后保留多边形的信息?让我试着解释一下:

我有一个包含这些数据的shapefile:

> head(mapa@data)
         ID      CD_GEOCODI   TIPO   CD_GEOCODB    NM_BAIRRO  CD_GEOCODS NM_SUBDIST CD_GEOCODD   NM_DISTRIT CD_GEOCODM   NM_MUNICIP     NM_MICRO                       NM_MESO
12228 33679 431490205000133 URBANO 431490205003 Cidade Baixa 43149020500       <NA>  431490205 PORTO ALEGRE    4314902 PORTO ALEGRE PORTO ALEGRE METROPOLITANA DE PORTO ALEGRE
12229 33680 431490205000134 URBANO 431490205003 Cidade Baixa 43149020500       <NA>  431490205 PORTO ALEGRE    4314902 PORTO ALEGRE PORTO ALEGRE METROPOLITANA DE PORTO ALEGRE
12230 33681 431490205000135 URBANO 431490205003 Cidade Baixa 43149020500       <NA>  431490205 PORTO ALEGRE    4314902 PORTO ALEGRE PORTO ALEGRE METROPOLITANA DE PORTO ALEGRE
12231 33682 431490205000136 URBANO 431490205003 Cidade Baixa 43149020500       <NA>  431490205 PORTO ALEGRE    4314902 PORTO ALEGRE PORTO ALEGRE METROPOLITANA DE PORTO ALEGRE
12232 33683 431490205000137 URBANO 431490205003 Cidade Baixa 43149020500       <NA>  431490205 PORTO ALEGRE    4314902 PORTO ALEGRE PORTO ALEGRE METROPOLITANA DE PORTO ALEGRE
12233 33684 431490205000138 URBANO 431490205003 Cidade Baixa 43149020500       <NA>  431490205 PORTO ALEGRE    4314902 PORTO ALEGRE PORTO ALEGRE METROPOLITANA DE PORTO ALEGRE
Run Code Online (Sandbox Code Playgroud)

而这个数据:

> head(data)
       CD_GEOCODI Population
1 431490205000133       1272
2 431490205000134        822
3 431490205000135       1085
4 431490205000136       1454
5 431490205000137        964
6 431490205000138        834
Run Code Online (Sandbox Code Playgroud)

我可以合并datamapa@data绘制它plot(),但我想使用ggplot2.但是fortify()输出没有任何原始变量.例如:

> head(fortify(mapa))
Regions defined for each Polygons
       long       lat order  hole piece   group    id
1 -51.22254 -30.03526     1 FALSE     1 12228.1 12228
2 -51.22332 -30.03648     2 FALSE     1 12228.1 12228
3 -51.22365 -30.03702     3 FALSE     1 12228.1 12228
4 -51.22482 -30.03610     4 FALSE     1 12228.1 12228
5 -51.22488 -30.03606     5 FALSE     1 12228.1 12228
6 -51.22476 -30.03591     6 FALSE     1 12228.1 12228
Run Code Online (Sandbox Code Playgroud)

两者(fortify和mapa @ data)都有一个id变量,但值不一样.所以,我的问题是:我如何将mapa@data信息传递给fortify()输出(或允许使用ggplot2的其他功能)

jlh*_*ard 14

由于您没有提供shapefile或数据,因此无法测试,但这样的事情应该有效:

# not tested...
library(plyr)      # for join(...)
library(rgdal)     # for readOGR(...)
library(ggplot2)   # for fortify(...)

mapa <- readOGR(dsn=".",layer="shapefile name w/o .shp extension")
map@data$id <- rownames(mapa@data)
mapa@data   <- join(mapa@data, data, by="CD_GEOCODI")
mapa.df     <- fortify(mapa)
mapa.df     <- join(mapa.df,mapa@data, by="id")

ggplot(mapa.df, aes(x=long, y=lat, group=group))+
  geom_polygon(aes(fill=Population))+
  coord_fixed()
Run Code Online (Sandbox Code Playgroud)

  • 我有一个关于由fortify(或broom :: tidy)生成的'id'列的问题.它从0开始.它对应于shapefile中空间对象的顺序吗? (3认同)

raf*_*ira 6

也许这是一个稍微简单的解决方案

library(dplyr)
library(ggplot2)

# fortify the shape file
  map.df <- fortify(shape, region ="CD_GEOCODI")

# merge data
  map.df <- left_join(map.df, data, by=c('id'='CD_GEOCODI'))
Run Code Online (Sandbox Code Playgroud)