Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anything wrong with my code? #2411

Open
liamxg opened this issue Jul 1, 2024 · 18 comments
Open

anything wrong with my code? #2411

liamxg opened this issue Jul 1, 2024 · 18 comments

Comments

@liamxg
Copy link

liamxg commented Jul 1, 2024

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
Copy link
Member

rsbivand commented Jul 1, 2024

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
Copy link
Author

liamxg commented Jul 1, 2024

Hi @rsbivand,

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

@rsbivand
Copy link
Member

rsbivand commented Jul 1, 2024

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
Copy link
Author

liamxg commented Jul 1, 2024

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

@rsbivand
Copy link
Member

rsbivand commented Jul 1, 2024

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
Copy link
Author

liamxg commented Jul 1, 2024

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
Copy link
Author

liamxg commented Jul 1, 2024

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

@rsbivand
Copy link
Member

rsbivand commented Jul 1, 2024

Send by email.

@liamxg
Copy link
Author

liamxg commented Jul 1, 2024

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

@rsbivand
Copy link
Member

rsbivand commented Jul 1, 2024

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

@liamxg
Copy link
Author

liamxg commented Jul 1, 2024

Thanks, sent.

@rsbivand
Copy link
Member

rsbivand commented Jul 1, 2024

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
Copy link
Author

liamxg commented Jul 1, 2024

@kadyb
Copy link
Contributor

kadyb commented Jul 1, 2024

@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
Copy link
Author

liamxg commented Jul 1, 2024

Dear @kadyb,

Amazing, thanks.

@rsbivand
Copy link
Member

rsbivand commented Jul 1, 2024

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
Copy link
Member

rsbivand commented Jul 1, 2024

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
Copy link
Contributor

kadyb commented Jul 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants