ropensci / osmplotr

Data visualisation using OpenStreetMap objects
https://docs.ropensci.org/osmplotr
135 stars 21 forks source link

Question #58

Closed nipnipj closed 1 year ago

nipnipj commented 1 year ago

Can I use a geom_sf layer on a map created with osmplotr? I'm trying the following.

bbox <- get_bbox (c(-0.13, 51.51, -0.11, 51.52))
dat_B <- extract_osm_objects (key = "building", bbox = bbox)
dat_H <- extract_osm_objects (key = "highway", bbox = bbox)

map <- bbox %>% 
  osm_basemap (bg = "gray20") %>% 
  add_osm_objects (dat_H, col = "gray70") %>% 
  add_osm_objects (dat_B, col = "gray40")

dt_points <- tibble(
  lng = c(-0.12,-0.125,-0.115),
  lat = c(51.515,51.5125,51.5175)
) %>%
  st_as_sf(coords = c("lng", "lat")) 

map +
  geom_sf(data = dt_points)

I'm getting Error: Discrete value supplied to continuous scale as error message, tho.

mpadge commented 1 year ago

No, geom_sf is not compatible with this package, so you either have to use geom_sf() only, and not this package, or use this package only, and not geom_sf(). But that just means intead of

map + geom_sf(data = dt_points)

you just need

map %>% add_osm_objects(data = dt_ponts)

(You also need to assign st_crs to dt_points before doing that , for example st_crs(dt_points) <- st_crs(dat_H).)

nipnipj commented 1 year ago

Thank you!