首页 > 解决方案 > 使用 ggplot2 绘制 shapefile

问题描述

我有一个与此非常相似的问题。我正在使用一个名为 shapefile 的文件PG,如下所示:

PG
class       : SpatialPolygonsDataFrame 
features    : 206 
extent      : -124.733, -66.94932, 24.54424, 49.38436  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 19
names       :               NAME, PHshare, Rank,  PUSA, DMA,    USTV, CPHShare, Hrank,     HTV,            markets, marketpop,   Dmoney,   Rmoney,              Dpc,              Rpc, ... 
min values  : ABILENE-SWEETWATER,       0,    1, 0.003, 500,    3890,   0.1498,     1,       0, ABILENE-SWEETWATER,         0,        0,        0,                0,                0, ... 
max values  :         ZANESVILLE,  14.979,  210, 6.553, 881, 7391940,        1,   210, 1817270,         ZANESVILLE,  20818031, 16769222, 13505141, 3214.19052665254, 4639.02342684636, ... 

我有兴趣用 ggplot 绘制多边形,并使用fill它来区分NAME. 考虑包含在 中的数据PG

    > head(PG@data)
             NAME PHshare Rank  PUSA DMA    USTV CPHShare Hrank     HTV         markets marketpop
0 PORTLAND-AUBURN   0.028   76 0.361 500  407560   0.9919   147    3360 PORTLAND-AUBURN    987088
1        NEW YORK   9.952    1 6.553 501 7391940   0.2493     2 1207480        NEW YORK  20818031
2      BINGHAMTON   0.017  156 0.123 502  138690   0.9961   166    2070      BINGHAMTON    350180
3           MACON   0.033  121 0.208 503  234690   0.9898   140    3970           MACON    660789
4    PHILADELPHIA   1.460    4 2.606 504 2939950   0.6770    18  177150    PHILADELPHIA   7979924
5         DETROIT   0.411   11 1.707 505 1925460   0.8564    42   49920         DETROIT   4835520
    Dmoney   Rmoney         Dpc       Rpc       AdsPC adrepshare  fraction split
0   520096   100553  526.899324  101.8683  628.767648  0.1620127 0.7480412     1
1        0        0    0.000000    0.0000    0.000000         NA 0.5341015     1
2      372        0    1.062311    0.0000    1.062311  0.0000000 1.0000000     0
3   308235        0  466.465089    0.0000  466.465089  0.0000000 1.0000000     0
4 15935528 13505141 1996.952352 1692.3897 3689.342029  0.4587240 0.4725686     1
5  5548735  5184179 1147.494995 1072.1037 2219.598719  0.4830169 1.0000000     0

我已经尝试了几件事,在这里我将它们包含在它们各自的错误消息中:

  1. ggplot() + geom_polygon(data = PG, aes(long, lat, group = group,fill=NAME), colour = alpha("darkred", 1/2), size = 0.7)产生Regions defined for each Polygons Error in FUN(X[[i]], ...) : object 'NAME' not found

2.ggplot() + geom_polygon(data = PG, aes(long, lat, group = group,fill=factor(NAME)), colour = alpha("darkred", 1/2), size = 0.7) 导致与上述相同的错误。

  1. ggplot() + geom_polygon(data = PG, aes(long, lat, group = group,fill=factor(PG@data$NAME)), colour = alpha("darkred", 1/2), size = 0.7)这导致Regions defined for each Polygons Error: Aesthetics must be either length 1 or the same as the data (322861): fill Run rlang::last_error() to see where the error occurred.

  2. 我也尝试过使用fortify,但我失去了NAME属性:

head(fortify(PG))
    Regions defined for each Polygons
           long      lat order  hole piece id group
    1 -69.02833 44.24904     1 FALSE     1  0   0.1
    2 -69.03648 44.23583     2 FALSE     1  0   0.1
    3 -69.03991 44.23422     3 FALSE     1  0   0.1
    4 -69.04394 44.21811     4 FALSE     1  0   0.1
    5 -69.04439 44.21343     5 FALSE     1  0   0.1
    6 -69.04689 44.20893     6 FALSE     1  0   0.1

标签: rggplot2geospatialshapefile

解决方案


推荐阅读