使用geom_sf读取shapefile和plot会返回错误

时间:2017-03-15 13:51:48

标签: r ggplot2 shapefile sf

我希望使用新的sf包和ggplot2中的geom_sf()绘制一些丹麦地图。我有一个来自公共地图服务的shapefile,共有4个文件(DK.shp,DK.dbf,DK.shf和DK.prj)。我已经提供了数据here in a zip-file(如果有更好的方法在Stack Overflow上共享文件,请告诉我,我不确定)。

在R中我尝试加载文件:

library(sf)

devtools::dev_mode(on = TRUE)
#devtools::install_github('tidyverse/ggplot2')

library(ggplot2)

d <- st_read('DK/DK.shp')

ggplot(d) +
  geom_sf()

nc <- st_read(system.file("shape/nc.shp", package="sf"))

ggplot(nc) +
  geom_sf()

geom_sf()按预期绘制buildin nc.shp文件,但DK.shp使其返回:

Error in x %*% Mat : non-conformable arguments
In addition: There were 50 or more warnings (use warnings() to see the first 50)

提到的50个警告都是以下类型:

1: In matrix(vec, ncol(m), nrow(m)) :
   data length [2] is not a sub-multiple or multiple of the number of rows [3]

我不确定这是我提供的数据文件的问题,还是sf或ggplot2中的错误。有关如何解决此问题的任何建议吗?

更新 查看st_geometry()返回的数据,我发现了以下内容:

x <- st_geometry(d[1,])
unlist(x)
# [1]  695222.0  695157.0  694574.5  693521.3  693590.6  693091.5  694813.6  694669.2  694190.6
# [10]  693295.8  692494.8  692655.9  693205.8  693998.4  695060.1  696658.0  697494.1  697847.6
# [19]  699947.6  700876.2  700936.4  702302.0  702727.3  702762.6  703223.3  703771.5  704180.7
# [28]  704991.5  704650.9  706955.0  708027.2  710597.9  710211.5  710128.9  710127.7  709773.2
# [37]  709965.2  708919.0  710098.1  710272.9  710586.6  712156.8  710876.0  710567.9  710041.6
# [46]  709731.5  708333.8  707001.3  707546.4  707193.8  706598.1  706370.5  704816.7  705153.2
# [55]  704208.8  704209.4  702438.4  702290.3  702017.5  701601.2  700813.9  700244.4  698705.2
# [64]  697584.8  695462.3  695222.0 6195729.1 6196153.6 6197058.5 6197735.0 6198228.8 6198687.0
# [73] 6199968.8 6200923.1 6201422.3 6201743.9 6203045.2 6204716.4 6205660.8 6205964.5 6207435.3
# [82] 6207247.1 6207387.4 6207111.0 6208095.2 6208737.5 6209758.0 6209820.5 6209795.2 6209941.6
# [91] 6209663.9 6209741.5 6209669.6 6210534.0 6211238.2 6211301.4 6211441.3 6211409.8 6210281.9
# [100] 6209192.3 6208759.1 6207688.6 6206301.1 6205454.5 6204600.1 6204566.5 6203154.7 6201390.7
# [109] 6199999.8 6199645.5 6199286.7 6199607.3 6199116.3 6199058.5 6198042.2 6197785.1 6198202.3
# [118] 6198169.9 6197827.8 6196050.1 6194848.5 6194665.8 6194474.5 6193641.8 6193609.7 6193729.3
# [127] 6193661.6 6196020.7 6197222.2 6196682.2 6195683.5 6195729.1    -999.0    -999.0    -999.0
# [136]    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0
# [145]    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0
# [154]    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0
# [163]    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0
# [172]    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0
# [181]    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0
# [190]    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0    -999.0

nx <- st_geometry(nc[1,])
unlist(nx)
# [1] -81.47276 -81.54084 -81.56198 -81.63306 -81.74107 -81.69828 -81.70280 -81.67000 -81.34530
# [10] -81.34754 -81.32478 -81.31332 -81.26624 -81.26284 -81.24069 -81.23989 -81.26424 -81.32899
# [19] -81.36137 -81.36569 -81.35413 -81.36745 -81.40639 -81.41233 -81.43104 -81.45289 -81.47276
# [28]  36.23436  36.27251  36.27359  36.34069  36.39178  36.47178  36.51934  36.58965  36.57286
# [37]  36.53791  36.51368  36.48070  36.43721  36.40504  36.37942  36.36536  36.35241  36.36350
# [46]  36.35316  36.33905  36.29972  36.27870  36.28505  36.26729  36.26072  36.23959  36.23436

所以我试图读取的shapefile包含不同格式的值。此外,所有-999值都发生了什么?我该如何处理?

1 个答案:

答案 0 :(得分:3)

我将此归档为问题;你的几何包含Z坐标,现在geom_sf不喜欢它。尝试

ggplot(st_zm(d)) + geom_sf()

从多边形节点中删除Z坐标。 (提出问题here