-
Notifications
You must be signed in to change notification settings - Fork 231
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
ggmap does not seem to work with ggplot2::geom_sf #160
Comments
I've made an attempt to solve the issue with pull request #162. Note that for the nc shape, I have a slight offset between the shape and the map... so that some issues may remain. |
Hi, Code and output below if it can help.
"Regular" plotting works well:
|
@gregleleu I also had this problem and never found the solution, unfortunately. Other map types work, just google has this problem. |
After struggling with this for a while, I found a way: The bounding box of the library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.3.0, proj.4 5.1.0
library(ggmap)
#> Loading required package: ggplot2
#> Google Maps API Terms of Service: http://developers.google.com/maps/terms.
#> Please cite ggmap if you use it: see citation("ggmap") for details.
library(ggplot2)
uk_lads <- st_read("https://opendata.arcgis.com/datasets/fab4feab211c4899b602ecfbfbc420a3_3.kml")
# Convert to 3857
uk_lads_3857 <- st_transform(uk_lads, 3857)
test_map_uk <- ggmap::get_map(location = unname(st_bbox(uk_lads)), source = "stamen")
# overwrite the bbox of the ggmap object with that from the uk map that has
# been transformed to 3857
attr(test_map_uk, "bb")$ll.lat <- st_bbox(uk_lads_3857)["ymin"]
attr(test_map_uk, "bb")$ll.lon <- st_bbox(uk_lads_3857)["xmin"]
attr(test_map_uk, "bb")$ur.lat <- st_bbox(uk_lads_3857)["ymax"]
attr(test_map_uk, "bb")$ur.lon <- st_bbox(uk_lads_3857)["xmax"]
ggmap::ggmap(test_map_uk) +
coord_sf(crs = st_crs(3857)) + # force it to be 3857
geom_sf(data = uk_lads_3857, inherit.aes = FALSE)
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one. Created on 2018-06-13 by the reprex package (v0.2.0). |
Edit: I posted a more general solution on StackOverflow which gets the bounding box from the ggmap object itself rather than from another source. library(ggplot2)
library(ggmap)
library(sf)
uk_lads <- st_read("https://opendata.arcgis.com/datasets/fab4feab211c4899b602ecfbfbc420a3_3.kml")
# Convert to 3857
uk_lads_3857 <- st_transform(uk_lads, 3857)
test_map_uk <- ggmap::get_map(location = unname(st_bbox(uk_lads)), source = "stamen")
# Define a function to fix the bbox to be in EPSG:3857
ggmap_bbox <- function(map) {
if (!inherits(map, "ggmap")) stop("map must be a ggmap object")
# Extract the bounding box (in lat/lon) from the ggmap to a numeric vector,
# and set the names to what sf::st_bbox expects:
map_bbox <- setNames(unlist(attr(map, "bb")),
c("ymin", "xmin", "ymax", "xmax"))
# Coonvert the bbox to an sf polygon, transform it to 3857,
# and convert back to a bbox (convoluted, but it works)
bbox_3857 <- st_bbox(st_transform(st_as_sfc(st_bbox(map_bbox, crs = 4326)), 3857))
# Overwrite the bbox of the ggmap object with the transformed coordinates
attr(map, "bb")$ll.lat <- bbox_3857["ymin"]
attr(map, "bb")$ll.lon <- bbox_3857["xmin"]
attr(map, "bb")$ur.lat <- bbox_3857["ymax"]
attr(map, "bb")$ur.lon <- bbox_3857["xmax"]
map
}
# Use the function:
test_map_uk <- ggmap_bbox(test_map_uk)
ggmap(test_map_uk) +
coord_sf(crs = st_crs(3857)) + # force the ggplot2 map to be in 3857
geom_sf(data = uk_lads_3857, inherit.aes = FALSE) |
I honestly think this should be implemented somewhere as long as there is no other solution available. |
I submitted a pull request (also including base maps from carto) but it adds an sf dependency which fails at build time because some libraries are not on the CI/CD system |
Any movement here? :o |
I honestly have been trying to figure out this hack for like an hour and I still can't get it to work |
what will be the value of crs if we use map of singapore |
@talhazarfarjutt, the underlying image of the map is in 3857, so we need to set the CRS of the overall ggplot to 3857 as it is in the code. Whatever crs your sf is in, geom_sf will reproject it to the ggplot crs (3857). So in theory nothing to change for Singapore. Is it not working? |
I feel like I've tried every possible combo of |
@SimonDedman st_contour segfaults on my computer (even after reinstalling and rebooting everything) so I can't run the reprex, but if that helps the steps I would try are:
|
Is there a fix available for this issue? Or could anybody recommend a work-around, or a link to an example that works - preferably for a UK map? Thank you, Phil |
Hack from above:
Then
and for each geom_sf: |
Hi all! Is this workaround still considered best practice for using ggmap and sf together? It seems like it still doesn't allow for all tweaks, e.g. disallowing cropping as mentioned by @SimonDedman. I see this question all over the internet and have been struggling with it for a while. Are there plans to implement this solution into the sf package eventually, or is there some reason that's not a good idea? Is there a different best practice that I'm missing? I absolutely respect that the sf developers have lots on their plate; I'm genuinely trying to understand the best guidance here, given that this seems like a pretty common thing to want to do. Thank you very much! |
i think all above mentioned libraries are mature now, i also faced problems but at the end problem was from my side. |
I have switched to {maptiles} to get the basemaps and {tidyterra} to plot them, it is a bit slower but integrates better with the {sf} and tidyverse ways of working. |
This sounds interesting Greg; I don't suppose you'd be prepared to share an example code chunk to demonstrate your workflow for this? Thanks! |
Here's an example. Hope that helps
|
Thanks @gregleleu , that's very nice. It looks like there's no scope to use Google's terrain image maps for the data provider in |
This might be an issue with
geom_sf
or an issue with how ggmap sets up the dataframe for ggmap or something else. I'm not sure, so I'm submitting it here and toggplot2
The text was updated successfully, but these errors were encountered: