Create Credentials (API key)
Enable Geolocation & Static Maps APIs
Select “APIs” tab on the left
Select “Maps Static API” -> ENABLE button
Select “Geolocation API” -> ENABLE button
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.5 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
tidyverse_logo()
## * __ _ __ . o * .
## / /_(_)__/ /_ ___ _____ _______ ___
## / __/ / _ / // / |/ / -_) __(_-</ -_)
## \__/_/\_,_/\_, /|___/\__/_/ /___/\__/
## * . /___/ o . *
# install package first time use -> install.packages('ggmap')
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
# Active Google Maps API. Use option 'write = TRUE' to make environment variable for all R sessions.
# Uncomment this line
# ggmap::register_google(key = "your copied Google map API key", write = TRUE)
Create a Google Map Static API object:
Specify a place, or center = c(longitude,latitude) coordinates
Zoom size = 10 is default for city level zoom, higher numbers are zoomed in more
Choose a map type = c(“terrain”, “satellite”, “roadmap”, “hybrid”) and color = c(“color”, “bw”)
Renders a 1280x1280 pixel map (ggmap/raster object).
ggmap::ggmap() to plot it
ggmap_sf <- ggmap::get_googlemap('San Francisco', zoom = 12)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=San%20Francisco&zoom=12&size=640x640&scale=2&maptype=terrain&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=San+Francisco&key=xxx
ggmap(ggmap_sf)
ggmap_center <- ggmap::get_googlemap(center = c(lon = -122.444810, lat = 37.762445),
zoom = 12,
maptype = 'hybrid',
color = "color")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=37.762445,-122.44481&zoom=12&size=640x640&scale=2&maptype=hybrid&key=xxx
ggmap(ggmap_center)
ggmap(get_googlemap('San Francisco', zoom = 13,
style = paste0("feature:road|element:all|color:0xC280E9")))
ggmap(get_googlemap('San Francisco', zoom = 13,
style = paste0("feature:road|element:all|color:0xC280E9",
"&style=feature:water|element:all|color:0x7FFF00")))
ggmap(get_googlemap('San Francisco', zoom = 13,
style = paste0("feature:all|element:labels|visibility:off")))
sf_trees <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-28/sf_trees.csv') %>%
drop_na() %>%
filter(legal_status == "DPW Maintained")
glimpse(sf_trees)
## Rows: 18,141
## Columns: 12
## $ tree_id <dbl> 30372, 30460, 30454, 30428, 30468, 30470, 30490, 53227, 5~
## $ legal_status <chr> "DPW Maintained", "DPW Maintained", "DPW Maintained", "DP~
## $ species <chr> "Ulmus parvifolia :: Chinese Elm", "Pittosporum undulatum~
## $ address <chr> "498x Arkansas St", "470 Union St", "381 Union St", "434 ~
## $ site_order <dbl> 1, 1, 1, 1, 2, 3, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
## $ site_info <chr> "Sidewalk: Curb side : Cutout", "Sidewalk: Curb side : Cu~
## $ caretaker <chr> "Private", "Private", "Private", "Private", "Private", "P~
## $ date <date> 1956-03-02, 1956-05-11, 1956-05-11, 1956-05-11, 1956-05-~
## $ dbh <dbl> 10, 19, 8, 13, 8, 8, 16, 13, 20, 12, 36, 14, 27, 11, 11, ~
## $ plot_size <chr> "3x3", "4x4", "3x3", "7x3", "3x3", "3x3", "3x3", "3x3", "~
## $ latitude <dbl> 37.76005, 37.80074, 37.80081, 37.80082, 37.80061, 37.8006~
## $ longitude <dbl> -122.3983, -122.4073, -122.4057, -122.4066, -122.4073, -1~
sf_trees %>%
ggplot(aes(longitude, latitude)) +
geom_point(color = 'green', alpha = 0.42)
ggmap(get_googlemap(center = c(lon = -122.444810, lat = 37.762445), zoom = 12,
style = paste0("feature:all|element:labels|visibility:off"))) +
geom_point(data = sf_trees, aes(longitude, latitude),
color = 'green',
size = 0.42,
alpha = 0.42)
ggmap(get_googlemap(center = c(lon = -122.444810, lat = 37.762445), zoom = 12,
style = paste0("feature:all|element:labels|visibility:off"))) +
stat_density2d(data = sf_trees, aes(longitude, latitude, fill = ..level..),
geom = "polygon", alpha = 0.42) +
scale_fill_gradient(low = "green", high = "red", guide = "none")
ggmap(get_googlemap(center = c(lon = -122.444810, lat = 37.762445),
zoom = 13,
maptype = 'hybrid',
style = paste0("feature:all|element:labels|visibility:off"))) +
stat_density2d(data = sf_trees,
aes(longitude, latitude, fill = ..level.., alpha = ..level..),
bins = 17, geom = 'polygon') +
scale_fill_gradient(low = "blue", high = "green", guide = "none") +
scale_alpha(guide = "none") +
labs(x = '', y = '', title = "San Francisco Trees Maintained by the Department of Public Works")
## Warning: Removed 3539 rows containing non-finite values (stat_density2d).