在R(ggplot)中使用fortify函数时出错 [英] Getting error while using fortify function in R (ggplot)

查看:2522
本文介绍了在R(ggplot)中使用fortify函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在R中使用函数fortify来绘制地图。但是我在处理数据时总是出错。使用以下代码读取形状文件:

 形状<  -  readShapeSpatial(Lond_City / lond_city.shp)

shapefile位于以下放置箱位置:
https://www.dropbox.com/sh/d4w6saailydtu1r/5oIa56xV6S

然后我尝试使用以下命令:

$ $ p $ code> shape1 < - fortify(shape,region =data)

在上面的代码中,我应该放置数据的位置。这是令人困惑的地方。我可以在R中打开数据或shapefile。但是,当我运行fortify行时,出现以下错误:
$ b


'[ .data.frame'(attr,region):未定义的列被选中


有人可以告诉我如何正确设置shape文件例如?我想使用fortify的原因是我想将形状文件与数据点结合起来。



非常感谢。



Jdbaba

解决方案

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

 > str(lon.shape)
具有5个插槽的正式类'SpatialPolygonsDataFrame'[包sp]
.. @ data:'data.frame':1 obs。 7个变量:
.. .. $ ons_label:chr00AA
.. .. $ name:chr伦敦金融城
.. .. $ label:chr02AA
.. .. $ X_max:num 533843
.. .. $ y_max:num 182198
.. .. $ X_min:num 0
.. .. $ y_min :num 0
.. @ polygons:1
.. .. $的列表:$ 5
的形式类'Polygons'[package sp']。 。@ Polygons:1
.. .. .. .. $:$ 5:
.. .. .. ..的正式班'Polygon'[packagesp]。 ... .. @ @ 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:chr0
.. .. .. .. @ area:num 3151465
.. @ plotOrder:int 1
.. @ bbox:num [1:2,1:2] 530967 180404 533843 182198
.. ..- attr(*,dimnames)= 2
列表.. .. .. $:chr [1:2]xy
.. .. .. $:chr [1:2]minmax
.. @ proj4string: CRS'[packagesp]带有1个槽
.. .. @ projargs:chr+ proj = tmerc + lat_0 = 49 + lon_0 = -2 + k = 0.9996012717 + x_0 = 400000 + y_0 =相反,尝试使用<$ c $ = $ c> name
,如下所示:

  lon.df < -  fortify(lon.shape, 




$ b 产生这个数据框的地方:

 >头(lon.df)
long lat订单孔组件编号
1 531026.9 181611.1 1 FALSE 1伦敦金融城1。伦敦金融城
531028.5 181611.2 2假1伦敦金融城1伦敦金融城
3 531036.1 181611.5 3假1伦敦金融城1.伦敦金融城
4 531074.0 181610.3 4假1伦敦金融城1。伦敦金融城
531107.0 181609.3 5假1城市伦敦金融城
6 531117.1 181608.9 6 FALSE 1伦敦金融城1。伦敦金融城



$ p
$ b $ p $ library $ r
library(rgdal)
$ ggplot2)
library(rgeos)

shape.dir< - c:/ test / london#在这里使用您的目录名称

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(color = )黑色,fill =grey80,size = 1)+
theme()

...导致以下内容:




I am trying to use the function fortify in R to plot the map. But I am always getting an error while processing the data. I read the shape file using the following code:

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

The shapefile can be found on the following dropbox location: https://www.dropbox.com/sh/d4w6saailydtu1r/5oIa56xV6S

Then I tried to use the following command :

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

In the above code, what should I put in Place of "data". This is the place which seems confusing. I can open the data or shapefile in R. However, when I run the fortify line I get the following error:

Error in '[.data.frame'(attr, , region) : undefined columns selected

Can anybody tell me how to properly fortify a shape file with example ? The reason I want to use fortify is that I want to combine the shape file with the data points.

Thank you so much.

Jdbaba

解决方案

Your problem is that you need to give fortify a column that actually exists. If you type str(lon.shape) from my example code below, you will see that there is no region column:

> 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" 

Instead, try using name, like this:

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

Which produces this data frame:

> 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

Here's one way to go about it, start to finish:

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()

...resulting in the following:

这篇关于在R(ggplot)中使用fortify函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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