-
Notifications
You must be signed in to change notification settings - Fork 2k
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
regression in geom_sf (+ geom_bin2d) after latest update #4527
Comments
This is probably the right location to report this, but I'm not sure whether you're reporting one issue or multiple issues. Could you try to simplify and really bring out one (or more) concrete problem(s)? If there are multiple problems, please open one issue per problem. Also, please simplify the reproducible example so it uses the minimum amount of packages required (ideally, only ggplot2). You can use one of the existing examples of |
The main issue is 3. which makes the 2d bin plot look bad. And I assume the performance issues are going to be related, since they happen only together. I tried a few thing and it seems to be related to assigned crs. library(ggplot2)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
csvData <- data.frame(lat = rnorm(10^5, 41.5, 0.1),
lon = rnorm(10^5, -72.75, 0.1))
pol <-st_polygon(list(rbind(c(-73.5,41.2), c(-73.5,41.5), c(-72,41.5), c(-72,41.2), c(-73.5,41.2))))
test_sf <- st_sf(st_sfc(pol))
ggplot(test_sf) +
geom_sf(color = "grey70", size = 0.1, fill = "transparent") +
geom_bin2d(data = csvData,
aes(x = lon,
y = lat,
fill = after_stat(log10(count))),
binwidth = 0.005) +
coord_sf(expand = FALSE, xlim = c(-73.7, -71.8), ylim = c(41.0, 42.0)) st_crs(test_sf) <- 4326
ggplot(test_sf) +
geom_sf(color = "grey70", size = 0.1, fill = "transparent") +
geom_bin2d(data = csvData,
aes(x = lon,
y = lat,
fill = after_stat(log10(count))),
binwidth = 0.005) +
coord_sf(expand = FALSE, xlim = c(-73.7, -71.8), ylim = c(41.0, 42.0))
#> Warning in st_cast.GEOMETRYCOLLECTION(X[[i]], ...): only first part of
#> geometrycollection is retained Created on 2021-06-22 by the reprex package (v2.0.0) |
Yes, the warning is unrelated. The sf package doesn't always behave correctly when no non-geometry column is available. The following fixes that issue: test_sf <- st_sf(x = 1, geometry = st_sfc(pol)) I can confirm the performance and rendering issues with |
The rendering errors go away if you set the The performance impact is probably due to coordinate transformation. However, in the example you provide, coordinates for the bins shouldn't get transformed, so I'll have to see what's going on there. |
@clauswilke should I wait with submitting 3.3.5 for including a fix for this? |
@clauswilke Thank you for looking into this. So relieved that you can reproduce this.. Having to set both |
@thomasp85 Let me try today to see if there's something simple I can do, though I already thought I had done the simple thing (let @Mashin6 That's just how polygons work. If you draw many polygons side-by-side, there can be rounding errors between them that show up as tiny gaps. You can use a single scale function and set the |
I'm quite sure I know what causes the difference in behavior @Mashin6 sees. For linear coordinate systems, @thomasp85 I think the problem is in this line: Line 25 in 389b864
It should just be:
If the fix is indeed that simple we should make it. I'm just worried that something else will blow up if we don't test properly. There must be a reason why I wrote it the way it is and I can't remember right now. Otherwise, we'd have to add more sophisticated logic in this line, and checking whether if the default crs is set does it match the crs: Line 272 in 389b864
Either way it should be a simple fix, I guess. |
With the proposed fix things appear fine and there is no performance difference between the three plots. library(ggplot2)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
csvData <- data.frame(
lat = rnorm(10^5, 41.5, 0.1),
lon = rnorm(10^5, -72.75, 0.1)
)
pol <-st_polygon(list(rbind(c(-73.5,41.2), c(-73.5,41.5), c(-72,41.5), c(-72,41.2), c(-73.5,41.2))))
test_sf <- st_sf(x = 1, geometry = st_sfc(pol))
ggplot(csvData) +
geom_bin2d(
aes(lon, lat, fill = after_stat(log10(count)))
) +
coord_fixed(xlim = c(-73.5, -72)) ggplot(csvData) +
geom_sf(data = test_sf) +
geom_bin2d(
aes(lon, lat, fill = after_stat(log10(count)))
) +
coord_sf(xlim = c(-73.5, -72)) test2_sf <- test_sf
st_crs(test2_sf) <- 4326
ggplot(csvData) +
geom_sf(data = test2_sf) +
geom_bin2d(
aes(lon, lat, fill = after_stat(log10(count)))
) +
coord_sf(xlim = c(-73.5, -72)) Created on 2021-06-23 by the reprex package (v1.0.0) |
Awesome! Thanks a ton! |
I am not sure if this should be posted here or in r-spatial/sf. Recently I updated several packages in my "R version 4.0.4 (2021-02-15) MacOS 11.4 " and noticed several weird behaviors when plotting 2d density over spatial map of countries.
In st_cast.GEOMETRYCOLLECTION(X[[i]], ...) : only first part of geometrycollection is retained
Created on 2021-06-22 by the reprex package (v2.0.0)
The text was updated successfully, but these errors were encountered: