Example: arcgeocoder and static maps
Combine arcgeocoder with sf and terra
Source:vignettes/articles/ex_static.Rmd
ex_static.Rmd
Example 1: sf objects
The following example shows how it is possible to create a nice static map with data retrieved with arcgeocoder and converting it to an sf object:
library(arcgeocoder)
library(dplyr)
library(sf) # spatial objects
library(ggplot2)
library(mapSpain) # sf objects of Spain
# McDonalds in Barcelona, Spain
mc <- arc_geo_multi("McDonalds",
city = "Barcelona",
region = "Catalonia", countrycode = "ES",
category = "Food", limit = 50,
custom_query = list(outFields = c("LongLabel", "Type,StAdd"))
)
# To sf
mc_sf <- st_as_sf(mc,
coords = c("lon", "lat"),
# here we have the wkid
crs = mc$latestWkid[1]
)
bcn <- esp_get_munic(munic = "Barcelona") %>%
st_transform(mc$latestWkid[1])
ggplot(bcn) +
geom_sf() +
geom_sf(data = mc_sf, color = "red")
# We can restrict the results to the bbox of BCN in the query
bbox <- st_bbox(bcn) %>% paste0(collapse = ",")
bbox
#> [1] "2.05362,41.32175,2.22717,41.46744"
mc2_sf <- arc_geo_multi("McDonalds",
city = "Barcelona",
region = "Catalonia", countrycode = "ES",
category = "Food", limit = 50,
custom_query = list(
outFields = c("LongLabel", "Type", "StAdd"),
searchExtent = bbox
)
) %>%
st_as_sf(coords = c("lon", "lat"), crs = mc$latestWkid[1])
ggplot(bcn) +
geom_sf() +
geom_sf(data = mc2_sf, color = "red")
Example 2: terra objects
We can add static map tiles thanks to maptiles package and tidyterra for plotting. The tiles themselves are represented here as terra objects:
library(maptiles)
library(tidyterra)
# Usually use crs 3857 for getting tiles
bcn_3857 <- st_transform(bcn, 3857)
osm_tiles <- get_tiles(bcn_3857, provider = "CartoDB.Positron", crop = TRUE)
ggplot() +
geom_spatraster_rgb(data = osm_tiles, maxcell = Inf) +
geom_sf(data = bcn, fill = NA, color = "black", linewidth = 1) +
geom_sf(data = mc2_sf, color = "red") +
coord_sf(crs = 3857) +
labs(caption = "(c) OpenStreetMap contributors, (c) CARTO")
Session info
Details
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.4.1 (2024-06-14 ucrt)
#> os Windows Server 2022 x64 (build 20348)
#> system x86_64, mingw32
#> ui RTerm
#> language en
#> collate English_United States.utf8
#> ctype English_United States.utf8
#> tz UTC
#> date 2024-09-11
#> pandoc 3.1.11 @ C:/HOSTED~1/windows/pandoc/31F387~1.11/x64/PANDOC~1.11/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> arcgeocoder * 0.2.0 2024-09-11 [1] local
#> bslib 0.8.0 2024-07-29 [1] RSPM
#> cachem 1.1.0 2024-05-16 [1] RSPM
#> class 7.3-22 2023-05-03 [3] CRAN (R 4.4.1)
#> classInt 0.4-10 2023-09-05 [1] RSPM
#> cli 3.6.3 2024-06-21 [1] RSPM
#> codetools 0.2-20 2024-03-31 [3] CRAN (R 4.4.1)
#> colorspace 2.1-1 2024-07-26 [1] RSPM
#> curl 5.2.2 2024-08-26 [1] RSPM
#> DBI 1.2.3 2024-06-02 [1] RSPM
#> desc 1.4.3 2023-12-10 [1] RSPM
#> digest 0.6.37 2024-08-19 [1] RSPM
#> dplyr * 1.1.4 2023-11-17 [1] RSPM
#> e1071 1.7-14 2023-12-06 [1] RSPM
#> evaluate 0.24.0 2024-06-10 [1] RSPM
#> fansi 1.0.6 2023-12-08 [1] RSPM
#> farver 2.1.2 2024-05-13 [1] RSPM
#> fastmap 1.2.0 2024-05-15 [1] RSPM
#> fs 1.6.4 2024-04-25 [1] RSPM
#> generics 0.1.3 2022-07-05 [1] RSPM
#> ggplot2 * 3.5.1 2024-04-23 [1] RSPM
#> glue 1.7.0 2024-01-09 [1] RSPM
#> gtable 0.3.5 2024-04-22 [1] RSPM
#> highr 0.11 2024-05-26 [1] RSPM
#> htmltools 0.5.8.1 2024-04-04 [1] RSPM
#> htmlwidgets 1.6.4 2023-12-06 [1] RSPM
#> jquerylib 0.1.4 2021-04-26 [1] RSPM
#> jsonlite 1.8.8 2023-12-04 [1] RSPM
#> KernSmooth 2.23-24 2024-05-17 [3] CRAN (R 4.4.1)
#> knitr 1.48 2024-07-07 [1] RSPM
#> lifecycle 1.0.4 2023-11-07 [1] RSPM
#> magrittr 2.0.3 2022-03-30 [1] RSPM
#> mapSpain * 0.9.2 2024-08-26 [1] RSPM
#> maptiles * 0.7.0 2024-01-11 [1] RSPM
#> munsell 0.5.1 2024-04-01 [1] RSPM
#> pillar 1.9.0 2023-03-22 [1] RSPM
#> pkgconfig 2.0.3 2019-09-22 [1] RSPM
#> pkgdown 2.1.0 2024-07-06 [1] any (@2.1.0)
#> png 0.1-8 2022-11-29 [1] RSPM
#> proxy 0.4-27 2022-06-09 [1] RSPM
#> purrr 1.0.2 2023-08-10 [1] RSPM
#> R.cache 0.16.0 2022-07-21 [1] RSPM
#> R.methodsS3 1.8.2 2022-06-13 [1] RSPM
#> R.oo 1.26.0 2024-01-24 [1] RSPM
#> R.utils 2.12.3 2023-11-18 [1] RSPM
#> R6 2.5.1 2021-08-19 [1] RSPM
#> ragg 1.3.2 2024-05-15 [1] RSPM
#> rappdirs 0.3.3 2021-01-31 [1] RSPM
#> Rcpp 1.0.13 2024-07-17 [1] RSPM
#> rlang 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.28 2024-08-17 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
#> scales 1.3.0 2023-11-28 [1] RSPM
#> sessioninfo * 1.2.2 2021-12-06 [1] any (@1.2.2)
#> sf * 1.0-17 2024-09-06 [1] RSPM
#> slippymath 0.3.1 2019-06-28 [1] RSPM
#> styler 1.10.3 2024-04-07 [1] RSPM
#> systemfonts 1.1.0 2024-05-15 [1] RSPM
#> terra 1.7-78 2024-05-22 [1] RSPM
#> textshaping 0.4.0 2024-05-24 [1] RSPM
#> tibble 3.2.1 2023-03-20 [1] RSPM
#> tidyr 1.3.1 2024-01-24 [1] RSPM
#> tidyselect 1.2.1 2024-03-11 [1] RSPM
#> tidyterra * 0.6.1 2024-06-08 [1] RSPM
#> units 0.8-5 2023-11-28 [1] RSPM
#> utf8 1.2.4 2023-10-22 [1] RSPM
#> vctrs 0.6.5 2023-12-01 [1] RSPM
#> withr 3.0.1 2024-07-31 [1] RSPM
#> xfun 0.47 2024-08-17 [1] RSPM
#> yaml 2.3.10 2024-07-26 [1] RSPM
#>
#> [1] D:/a/_temp/Library
#> [2] C:/R/site-library
#> [3] C:/R/library
#>
#> ──────────────────────────────────────────────────────────────────────────────