使用fortify {ggplot2}为R中的空间对象转换地图数据为数据帧 [英] Convert map data to data frame using fortify {ggplot2} for spatial objects in R

查看:2087
本文介绍了使用fortify {ggplot2}为R中的空间对象转换地图数据为数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个脚本没有任何问题,但现在强化{ggplot2}命令给我一个错误信息。任何暗示什么可能是问题会很好!我使用fortify命令可以使用ggplot2 geom_map shapefile。



以下是我的脚本和链接,用于下载数据。

  ################################### #################### 
########################## #############################
rm(list = ls(all = TRUE))#clear workspace
getwd()
#upload包
库(映射)
库(mapdata)
库(gridExtra)
库(rgdal)
库(rgeos) )
库(ggplot2)
库(sp)
库(maptools)
gpclibPermit()

#setwd(... / FAO )粮农组织的数据是主要捕捞区域部门
>粮农组织< - readOGR(dsn =fao,layer =World_Fao_Zones)
带驱动程序的OGR数据源:ESRI Shapefile
来源:fao,图层:World_Fao_Zones
含19特征和1字段
特征类型:带2维的wkbPolygon
> (FAO); dim(FAO)
[1]zone
[1] 19 1
> str(粮农组织,最高级别= 2)
带5个槽
的正式班'SpatialPolygonsDataFrame'[包sp] .. @ data:'data.frame':19 obs。 1变量:
.. @ polygons:19
列表.. @ plotOrder:int [1:19] 18 2 17 4 12 3 13 11 5 6 ...
.. @ bbox:num [1:2,1:2] -180 -85.5 180 90
.. ..- attr(*,dimnames)= 2
列表.. @ proj4string:Formal class'CRS'[packagesp] with 1 slots
> plot(FAO)

FAO.df< - as.data.frame(粮农组织)#将shapefile转换为数据框以便以后合并
>强制使用ggplot2
函数(类,fdef,mtable)中的错误:
无法找到函数proj4string的继承方法(FAO,region =id ,用于签名NULL

这里有一个链接来下载数据粮农组织数据
谢谢!

解决方案

我在 ggplot2 这里是我如何在底部的版本和会话信息中做到的。



ggplot2中的地图





h2>

  rm(list = ls(all = TRUE))#clear workspace 

图书馆(maptools)
图书馆(gpclib)
图书馆(ggplot2)

形状< -readShapeSpatial(./fao / World_Fao_Zones.shp)
形状@ data $ id< - rownames(shape @ data)
shape.fort< - fortify(shape,region ='id')
shape.fort< -shape.fort [order(shape.fort
ggplot(data = shape.fort,aes(long,lat,group = group))+
geom_polygon(color ='black',
fill ='white' )+
theme_bw()



会话



 > sessionInfo()
R版本2.15.0(2012-03-30)
平台:x86_64-apple-darwin9.8.0 / x86_64(64位)

语言环境:
[1] C / en_US.UTF-8 / C / C / C / C

附加基础软件包:
[1]网格统计图形grDevices utils
[ 6] datasets方法base

其他附加包:
[1] mapproj_1.1-8.3 gpclib_1.5-1 maptools_0.8-14
[4] lattice_0.20- 6 foreign_0.8-49 rgeos_0.2-5
[7] stringr_0.6 sp_0.9-99 gridExtra_0.9
[10] mapdata_2.2-1 ggplot2_0.9.0 maps_2.2-5

通过命名空间加载(并未附加):
[1] MASS_7.3-17 RColorBrewer_1.0-5 colorspace_1.1-1
[4] dichromat_1.2- 4 digest_0.5.2 memoise_0.1
[7] munsell_0.3 plyr_1.7.1 proto_0.3-9.2
[10] reshape2_1.2.1 scales_0.2.0 tools_2.15.0


I use to be able to run this script without any problem, but now the fortify {ggplot2} command gives me an error message. Any hint of what might be the problem would be great! I used the fortify command to be able to geom_map the shapefile using ggplot2.

Below is my script and link for downloading the data.

#######################################################
#######################################################
rm(list = ls(all = TRUE))#clear workspace
getwd()
#upload packages
library(maps)
library(mapdata)
library(gridExtra)
library(rgdal)
library(rgeos)
library(ggplot2)
library(sp)
library(maptools)
gpclibPermit()

#setwd(".../FAO") FAO data are major fishing area divisions
> FAO<- readOGR(dsn="fao", layer="World_Fao_Zones")
OGR data source with driver: ESRI Shapefile 
Source: "fao", layer: "World_Fao_Zones"
with 19 features and 1 fields
Feature type: wkbPolygon with 2 dimensions
> names(FAO);dim(FAO)
[1] "zone"
[1] 19  1
> str(FAO,max.level=2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 19 obs. of  1 variable:
  ..@ polygons   :List of 19
  ..@ plotOrder  : int [1:19] 18 2 17 4 12 3 13 11 5 6 ...
  ..@ bbox       : num [1:2, 1:2] -180 -85.5 180 90
  .. ..- attr(*, "dimnames")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
> plot(FAO)

FAO@data$id = rownames(FAO@data)
FAO.df <- as.data.frame(FAO)# convert shapefile to dataframe to merge later
> FAO_fort <- fortify(FAO, region="id")# fortify to plot with ggplot2 
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function "proj4string", for signature "NULL"

Here's a link to download the data FAO data. Thank you!

解决方案

I got it to work in ggplot2 and here is how I did it with the version and session info at the bottom.

The Map in ggplot2

The Code

rm(list = ls(all = TRUE)) #clear workspace

library(maptools)
library(gpclib)
library(ggplot2)

shape<-readShapeSpatial("./fao/World_Fao_Zones.shp") 
shape@data$id <- rownames(shape@data)
shape.fort <- fortify(shape, region='id') 
shape.fort<-shape.fort[order(shape.fort$order), ] 
ggplot(data=shape.fort, aes(long, lat, group=group)) + 
    geom_polygon(colour='black',
                 fill='white') +
    theme_bw()

The Session

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
    [1] C/en_US.UTF-8/C/C/C/C

attached base packages:
    [1] grid      stats     graphics  grDevices utils    
[6] datasets  methods   base     

other attached packages:
    [1] mapproj_1.1-8.3 gpclib_1.5-1    maptools_0.8-14
[4] lattice_0.20-6  foreign_0.8-49  rgeos_0.2-5    
[7] stringr_0.6     sp_0.9-99       gridExtra_0.9  
[10] mapdata_2.2-1   ggplot2_0.9.0   maps_2.2-5     

loaded via a namespace (and not attached):
    [1] MASS_7.3-17        RColorBrewer_1.0-5 colorspace_1.1-1  
[4] dichromat_1.2-4    digest_0.5.2       memoise_0.1       
[7] munsell_0.3        plyr_1.7.1         proto_0.3-9.2     
[10] reshape2_1.2.1     scales_0.2.0       tools_2.15.0

这篇关于使用fortify {ggplot2}为R中的空间对象转换地图数据为数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆