Skip to content

geom_sf compatibility trick#162

Open
lepennec wants to merge 1 commit intodkahle:masterfrom
lepennec:master
Open

geom_sf compatibility trick#162
lepennec wants to merge 1 commit intodkahle:masterfrom
lepennec:master

Conversation

@lepennec
Copy link

@lepennec lepennec commented Jun 8, 2017

Moving the x/y aesthetic seems to make ggmap compatible with the new geom_sf.

Note that the crs of the geom_sf should be set to "+init=epsg:4326" in order to be compatible with the google map crs.

Example:

library(tidyverse)
library(sf)
library(ggmap)

data(meuse, package = "sp") # load data.frame from sp

meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) %>%
  st_transform(crs = 4326)

meuse_bbox <- st_bbox(meuse_sf)

meuse_location <- c(lon = (meuse_bbox["xmin"]+meuse_bbox["xmax"])/2,
                 lat = (meuse_bbox["ymin"]+meuse_bbox["ymax"])/2)

meuse_map <- get_map(meuse_location, zoom = 13)

ggmap(meuse_map) + geom_sf(data = meuse_sf) +
  coord_sf(xlim = c(meuse_bbox["xmin"] - .1 * (meuse_bbox["xmax"]-meuse_bbox["xmin"]),
                    meuse_bbox["xmax"] + .1 * (meuse_bbox["xmax"]-meuse_bbox["xmin"])),
           ylim = c(meuse_bbox["ymin"] - .1 * (meuse_bbox["ymax"]-meuse_bbox["ymin"]),
                    meuse_bbox["ymax"] + .1 * (meuse_bbox["ymax"]-meuse_bbox["ymin"])))

yields

rplot

Note that I had to apply pull request #154 in order to make ggmap works with the github version of ggplot2.

move aes into geom_blank
Beware: the crs of sf should be "+init=epsg:4326"
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

Successfully merging this pull request may close these issues.

1 participant