r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.3k stars 289 forks source link

anything wrong with my code? #2411

Open liamxg opened 5 days ago

liamxg commented 5 days ago

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
rsbivand commented 5 days ago

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.

liamxg commented 5 days ago

Hi @rsbivand,

Thanks. I have attached the file. [Uploading wlf_nhr_fl_dfomasterlist_20190418.zip…]()

rsbivand commented 5 days ago

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.

liamxg commented 5 days ago

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

rsbivand commented 5 days ago

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.

liamxg commented 5 days ago

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 commented 5 days ago

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

rsbivand commented 5 days ago

Send by email.

liamxg commented 5 days ago

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

rsbivand commented 5 days ago

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

liamxg commented 5 days ago

Thanks, sent.

rsbivand commented 5 days ago

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.

liamxg commented 4 days ago

Dear @rsbivand,

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

kadyb commented 4 days ago

@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")
liamxg commented 4 days ago

Dear @kadyb,

Amazing, thanks.

rsbivand commented 4 days ago

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 commented 4 days ago

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.

kadyb commented 4 days ago

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.