Search code examples
rggplot2shapefiler-sf

Reading a shapefile and plot with geom_sf returns error


I wish to plot some maps of Denmark using the new sf package and geom_sf() from ggplot2. I have a shapefile from a public map service, with a total of 4 files (DK.shp, DK.dbf, DK.shf and DK.prj). I have made the data available here in a zip-file (let me know if there is a better way to share files on Stack Overflow, I was not sure).

In R I try to load the file:

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() plots the buildin nc.shp file just as expected, but DK.shp makes it return:

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

The 50 warnings mentioned are all of the type:

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

I am not sure whether this is a problem with the data-file I supply, or a bug in either sf or ggplot2. Any suggestions on how to resolve this?

Update Looking into the data returned by st_geometry() i found the following:

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

So the shapefile I am trying to read contains the values in a different format or something. Also, what is going on with all the -999 values? How to I deal with this?


Solution

  • I'll file this as an issue; your geometry contains Z coordinates, and right now geom_sf doesn't like that. Try

    ggplot(st_zm(d)) + geom_sf()
    

    to drop the Z coordinate from your polygon nodes. (Issue raised here)