sf icon indicating copy to clipboard operation
sf copied to clipboard

anything wrong with my code?

Open liamxg opened this issue 1 year ago • 18 comments

Dear @edzer,

Please see below:

shape <- sf::st_read("wlf_nhr_fl_dfomasterlist_20190418.shp")

ggplot() + 
+     geom_sf(data = shape, size = 1.5, color = "black", fill = "cyan1") + 
+     ggtitle("Lake Erie Outline") + 
+     coord_sf()
Error in grid.Call.graphics(C_setviewport, vp, TRUE) : 
  non-finite location and/or size for viewport
In addition: Warning messages:
1: Position guide is perpendicular to the intended axis.
ℹ Did you mean to specify a different guide `position`? 
2: Position guide is perpendicular to the intended axis.
ℹ Did you mean to specify a different guide `position`? 
3: In cos(mid_y * pi/180) : NaNs produced

liamxg avatar Jul 01 '24 00:07 liamxg

This is not reproducible since a minimal dataset is not provided. The problem is most likely your probably invalid geometries. A proper traceback() would help, as it is possible that the shape object is projected and your chosen plotting package imposes by default the plotting of a cartographically irrelevant grid in spherical coordinates.

rsbivand avatar Jul 01 '24 06:07 rsbivand

Hi @rsbivand,

Thanks. I have attached the file. Uploading wlf_nhr_fl_dfomasterlist_20190418.zip…

liamxg avatar Jul 01 '24 08:07 liamxg

No file attached at all. Probably it is large and was not uploaded when you sent the comment. Create the smallest possible subset of the geometries that reproduces the problem.

rsbivand avatar Jul 01 '24 08:07 rsbivand

Dear @rsbivand, Can I send it to your email, it is very important to me, thanks.

liamxg avatar Jul 01 '24 08:07 liamxg

If it is too large for github, it will be too large for email. Probably too laarge anyway, and I never use ggplot2 anyway. If you cannot subset it, maybe read https://r.geocompx.org/ thoroughly first. Do you actually need to plot this object using ggplot2? Does it plot with the sf plot method? You can put the object on Google Drive if you like, but I'm unsure I can help with ggplot2.

rsbivand avatar Jul 01 '24 08:07 rsbivand

Dear @rsbivand,

I want to use this command:

ggplot() + 
  geom_sf(data = shape, size = 1.5, color = "black", fill = "cyan1") + 
  ggtitle("Lake Erie Outline") + 
  coord_sf()

liamxg avatar Jul 01 '24 08:07 liamxg

the file is just 611KB, not too large to send by email.

liamxg avatar Jul 01 '24 08:07 liamxg

Send by email.

rsbivand avatar Jul 01 '24 08:07 rsbivand

Thanks, could you please tell me your email, thanks.

liamxg avatar Jul 01 '24 09:07 liamxg

packageDescription("spdep")["Maintainer"]. Note that github issues are world-readable so unsuitable for publishing email addresses. My email is easy to find online.

rsbivand avatar Jul 01 '24 09:07 rsbivand

Thanks, sent.

liamxg avatar Jul 01 '24 11:07 liamxg

The shapefile has been corrupted:

> st_bbox(shape)
          xmin           ymin           xmax           ymax 
-1.797693e+308 -1.797693e+308   1.796500e+02   1.248330e+02 

Both xmin and ymin are very large negative numbers, and ymax is too large, it is 124.833 which is well beyond 90, which is the North pole. Check with the source for your shapefile, it is not usable under any circumstances.

rsbivand avatar Jul 01 '24 11:07 rsbivand

Dear @rsbivand,

I used the file: https://data.humdata.org/dataset/global-active-archive-of-large-flood-events-dfo

liamxg avatar Jul 01 '24 12:07 liamxg

@liamxg, I think you can remove the outlier coordinates and then at least plot the points that have the correct coordinates.

library("sf")
library("ggplot2")

x = read_sf("wlf_nhr_fl_dfomasterlist_20190418.shp")
crds = st_coordinates(x)
idx = crds[, 1] < -180 | crds[, 1] > 180 | crds[, 2] < -90 | crds[, 2] > 90
# this removes the geometries (coordinates), but the records will still be in the data frame
x$geometry[idx] = st_point()

ggplot() + 
  geom_sf(data = x, size = 1.5, color = "black")

kadyb avatar Jul 01 '24 14:07 kadyb

Dear @kadyb,

Amazing, thanks.

liamxg avatar Jul 01 '24 14:07 liamxg

But do we know that the points with invalid coordinates really should have been invalid? Most of the points are invalid:

> table(idx)
idx
FALSE  TRUE 
  924  3105 

This really does not look like the outline of Lake Eire. The link to the actual dataset description would be helpful, is it: https://data.humdata.org/dataset/1fd855de-57c6-42b3-83e1-9cf989b0f70d/resource/984cc240-b2b7-4266-9f61-5715a9e10ff5. Why are the geometries of 3105 records suppressed?

rsbivand avatar Jul 01 '24 14:07 rsbivand

The shapefile at https://floodobservatory.colorado.edu/temp/ is of extent polygons, but:

xx <- st_read("FloodArchive_region.shp")
sf_use_s2(FALSE)
plot(st_centroid(st_make_valid(st_geometry(xx))))

looks less odd. Still not Lake Eire, but a better map of large floods.

rsbivand avatar Jul 01 '24 15:07 rsbivand

Why are the geometries of 3105 records suppressed?

I'm just guessing. Maybe they started to get information about the spatial extent of the floods since certain time (e.g. based on satellite images), and before that it was not taken into account. Then the Centroid_x and Centroid_Y columns assume 0. Some general textual information about the extent of the floods can be found in the Detailed_L column.

kadyb avatar Jul 01 '24 19:07 kadyb