得到错误而在R(ggplot)使用设防功能(Getting error while using fo

2019-08-07 03:31发布

我想使用的功能中的R巩固绘制地图。 但是,我总是得到一个错误,同时处理数据。 我使用下面的代码读取形文件:

shape <- readShapeSpatial("Lond_City/lond_city.shp")

shape文件可在以下Dropbox的位置找到: https://www.dropbox.com/sh/d4w6saailydtu1r/5oIa56xV6S

然后我试着用下面的命令:

shape1 <- fortify(shape,region="data")

在上面的代码,我应该把在“数据”的地方。 这看起来是令人困惑的地方。 我可以然而打开R.数据或shape文件,当我运行设防线我得到以下错误:

错误在 '[.data.frame'(ATTR,区域):选择的未定义的列

谁能告诉我如何正确使用强化例如形状文件? 我想使用Fortify的原因是,我想要的形状文件,数据点结合起来。

非常感谢。

Jdbaba

Answer 1:

你的问题是,您需要给fortify实际存在的列。 如果您键入str(lon.shape)从我下面的示例代码,你会看到,有没有region列:

> str(lon.shape)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 1 obs. of  7 variables:
  .. ..$ ons_label: chr "00AA"
  .. ..$ name     : chr "City of London"
  .. ..$ label    : chr "02AA"
  .. ..$ X_max    : num 533843
  .. ..$ y_max    : num 182198
  .. ..$ X_min    : num 0
  .. ..$ y_min    : num 0
  ..@ polygons   :List of 1
  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
  .. .. .. ..@ Polygons :List of 1
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 532464 181220
  .. .. .. .. .. .. ..@ area   : num 3151465
  .. .. .. .. .. .. ..@ hole   : logi FALSE
  .. .. .. .. .. .. ..@ ringDir: int 1
  .. .. .. .. .. .. ..@ coords : num [1:771, 1:2] 531027 531029 531036 531074 531107 ...
  .. .. .. ..@ plotOrder: int 1
  .. .. .. ..@ labpt    : num [1:2] 532464 181220
  .. .. .. ..@ ID       : chr "0"
  .. .. .. ..@ area     : num 3151465
  ..@ plotOrder  : int 1
  ..@ bbox       : num [1:2, 1:2] 530967 180404 533843 182198
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs" 

相反,尝试使用name ,如:

lon.df <- fortify(lon.shape, region = "name")

其产生该数据帧:

> head(lon.df)
      long      lat order  hole piece            group             id
1 531026.9 181611.1     1 FALSE     1 City of London.1 City of London
2 531028.5 181611.2     2 FALSE     1 City of London.1 City of London
3 531036.1 181611.5     3 FALSE     1 City of London.1 City of London
4 531074.0 181610.3     4 FALSE     1 City of London.1 City of London
5 531107.0 181609.3     5 FALSE     1 City of London.1 City of London
6 531117.1 181608.9     6 FALSE     1 City of London.1 City of London

这里的去做,从头到尾的一种方式:

library(rgdal)
library(ggplot2)
library(rgeos)

shape.dir <- "c:/test/london" # use your directory name here

lon.shape <- readOGR(shape.dir, layer = "lond_city")
lon.df <- fortify(lon.shape, region = "name")

ggplot(lon.df, aes(x = long, y = lat, group = group)) +
    geom_polygon(colour = "black", fill = "grey80", size = 1) +
    theme()

...导致以下:



Answer 2:

你应该把label代替data

require(maptools); require(ggplot2)
shape <- readShapeSpatial("Lond_City/lond_city.shp")
shape1 <- fortify(shape,region="label")

要了解这一点我检查的数据元素shape

> shape@data
  ons_label           name label    X_max    y_max X_min y_min
0      00AA City of London  02AA 533842.7 182198.4     0     0

这表明ons_labelname也将有工作,可能更适合你的目的。

请注意,您的形状文件是有点不同寻常,因为似乎只有一组。



文章来源: Getting error while using fortify function in R (ggplot)
标签: r ggplot2