我过去可以毫无问题地运行此脚本,但现在fortify {ggplot2}命令给出了一条错误消息。任何可能是什么问题的提示都会很好!我使用了geom_map命令,以便能够使用ggplot2来增强shapefile。
下面是我的脚本和下载数据的链接。
#######################################################
#######################################################
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"这里有一个下载data FAO data的链接。谢谢!
发布于 2017-05-15 19:27:14
Fortify很可能是deprecated。一种新的替代方案是broom (请参阅文档here)。它的使用非常简单:
# Load shapefile
FAO <- readOGR(dsn="fao", layer="World_Fao_Zones")
#Convert
FAO_df <- tidy(FAO)不幸的是,您上传的文件已经不存在了,所以我无法演示您示例中的命令。
https://stackoverflow.com/questions/11052544
复制相似问题