This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#install.packages('tmap')
library(tmap)
#install.packages('sf')
library(sf)
## Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
#install.packages('tmap')
library(osmdata)
## Warning: package 'osmdata' was built under R version 4.4.3
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
#install.packages('tmap')
library(osmdata)
#install.packages('leaflet')
library(leaflet) # for visualizing leaflet maps
# Get all food-related amenities in Ann Arbor
q <- opq(bbox = getbb("Ann Arbor, US")) %>%
add_osm_features(features = c(
"amenity" = "restaurant",
"amenity" = "bar",
"amenity" = "biergarden",
"amenity" = "fast_food",
"amenity" = "food_court",
"amenity" = "cafe",
"amenity" = "pub",
"amenity" = "ice_cream"
)) %>%
osmdata_sf()
# only take point geometry and drop entries that do not have values in name and amenity category
amenity_point <- q$osm_points %>%
select(osm_id, name, amenity, geometry) %>%
drop_na(name, amenity)
head(amenity_point)
## Simple feature collection with 6 features and 3 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -83.74839 ymin: 42.25572 xmax: -83.68812 ymax: 42.28785
## Geodetic CRS: WGS 84
## osm_id name amenity geometry
## 305088494 305088494 Casey's Tavern pub POINT (-83.74434 42.28785)
## 541900696 541900696 Starbucks cafe POINT (-83.68812 42.25572)
## 560917683 560917683 Good Time Charley's bar POINT (-83.73485 42.27482)
## 695043656 695043656 Real Seafood Company restaurant POINT (-83.74839 42.27837)
## 748964467 748964467 Pita Kabob Grill restaurant POINT (-83.74112 42.27797)
## 749007070 749007070 Ashley's Ann Arbor pub POINT (-83.74095 42.2781)
amenity_colors <- c(
"pub" = "red",
"cafe" = "blue",
"bar" = "darkgreen",
"restaurant" = "orange",
"fast_food" = "brown",
"ice_cream" = "purple"
)
# Initializes the Leaflet map using the amenity_point dataset.
leaflet(data = amenity_point) %>%
# Use the CartoDB.Positron basemap instead of OSM basemap
addProviderTiles(providers$CartoDB.Positron) %>%
# Adds interactive markers to the map.
addCircleMarkers(
# Extracts longitude values from the sf geometry column.
lng = st_coordinates(amenity_point)[,1],
# Extracts latitude values from the sf geometry column.
lat = st_coordinates(amenity_point)[,2],
# Displays a popup with amenity details when clicked.
popup = ~paste0("<b>", name, "</b><br>", amenity),
# Defines marker size.
radius = 3,
# Vectorize color mapping
color = ~unname(amenity_colors[amenity]),
# Make markers slightly transparent for better visibility
fillOpacity = 1,
clusterOptions = markerClusterOptions()
) %>%
addLegend(
# Position the legend at the bottom right of the map
"bottomright",
# Use the predefined colors assigned to each amenity type
colors = amenity_colors,
# Display corresponding labels (amenity names)
labels = names(amenity_colors),
# Add a title to the legend
title = "Amenity Type",
# Ensure the legend is fully visible
opacity = 1
) %>%
# Set the initial map view centered on Ann Arbor
setView(lng = -83.7430, lat = 42.2808, zoom = 12)