我曾经能够毫无问题地运行此脚本,但现在 fortify {ggplot2} 命令给了我一条错误消息。任何可能是问题的提示都会很棒!我使用了 fortify 命令,以便能够使用 ggplot2 对 shapefile 进行 geom_map。
下面是我的脚本和下载数据的链接。
#######################################################
#######################################################
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"
这是下载数据粮农组织数据的链接。谢谢!