library(readr)
read_csv("ru.csv")
ru <-summary(ru)
## city lat lng country
## Length:723 Min. :42.07 Min. : 19.92 Length:723
## Class :character 1st Qu.:49.74 1st Qu.: 38.30 Class :character
## Mode :character Median :54.75 Median : 43.17 Mode :character
## Mean :53.44 Mean : 53.15
## 3rd Qu.:56.65 3rd Qu.: 59.70
## Max. :70.77 Max. :177.52
##
## iso2 admin_name capital population
## Length:723 Length:723 Length:723 Min. : 3188
## Class :character Class :character Class :character 1st Qu.: 32176
## Mode :character Mode :character Mode :character Median : 52004
## Mean : 159931
## 3rd Qu.: 107689
## Max. :17125000
## NA's :97
## population_proper
## Min. : 3188
## 1st Qu.: 32176
## Median : 51768
## Mean : 153412
## 3rd Qu.: 107689
## Max. :13200000
## NA's :97
# Load the leaflet library
#install.packages("leaflet")
library(leaflet)
library(dplyr)
# Create a leaflet map with default map tile using addTiles()
leaflet() %>%
addTiles()
leaflet() %>%
addProviderTiles("Esri")
For the next examples I will use CartoDB provider tiles, but feel free to try others.
head(ru, 1)
city | lat | lng | country | iso2 | admin_name | capital | population | population_proper |
---|---|---|---|---|---|---|---|---|
Moscow | 55.7558 | 37.6178 | Russia | RU | Moskva | primary | 17125000 | 13200000 |
leaflet() %>%
addProviderTiles("CartoDB") %>%
setView(lng = 37.6178, lat = 55.7558, zoom = 6)
leaflet(options = leafletOptions(
# Set minZoom and dragging
minZoom = 12, dragging = TRUE)) %>%
addProviderTiles("CartoDB") %>%
# Set default zoom level
setView(lng = 37.6178, lat = 55.7558, zoom = 12) %>%
# Set max bounds of map
setMaxBounds(lng1 = 37.6178 + .05,
lat1 = 55.7558 + .05,
lng2 = 37.6178 - .05,
lat2 = 55.7558 - .05)
# Plot Moscow with zoom of 12
#leaflet() %>%
#addProviderTiles("CartoDB") %>%
#addMarkers(lng = 37.6178, lat = 55.7558) %>%
#setView(lng = 37.6178, lat = 55.7558, zoom = 12)
# Plot first-level admin capitals' locations
ru %>% filter(capital == "admin")
ru_admin =
leaflet() %>%
addProviderTiles("CartoDB") %>%
addMarkers(lng = ru_admin$lng, lat = ru_admin$lat)
# Store leaflet hq map in an object called map
leaflet() %>%
map <- addProviderTiles("CartoDB") %>%
# Use dc_hq to add the hq column as popups
addMarkers(lng = ru$lng[1], lat = ru$lat[1],
popup = ru$city[1])
# Center the view of map on the Moscow with a zoom of 5
map %>%
map_zoom <- setView(lng = 37.6178, lat = 55.7558,
zoom = 5)
# Print map_zoom
map_zoom
# Use addCircleMarkers() to plot each first-level admin capitals as a circle
leaflet() %>%
map2 <- addProviderTiles("CartoDB") %>%
addCircleMarkers(lng = ru_admin$lng, lat = ru_admin$lat)
map2
# Change the radius of each circle to be 2 pixels and the color to red
leaflet() %>%
map1 <- addProviderTiles("CartoDB") %>%
addCircleMarkers(lng = ru_admin$lng, lat = ru_admin$lat,
radius = 2, color = "yellow")
map1
# Make a color palette called pal for the values of `capital` using `colorFactor()`
# Colors: "red", "blue", and "#9b4a11" for "capital", "admin", and "minor" cities, respectively
colorFactor(palette = c("red", "blue", "yellow"),
pal <-levels = c("primary", "admin", "minor"))
ru %>% filter(capital == "primary" | capital == "admin" | capital == "minor")
ru1 <-
# Add circle markers that color colleges using pal() and the values of capital
leaflet() %>%
map3 <- addProviderTiles("CartoDB") %>%
addCircleMarkers(data = ru1, radius = 2, popup = ~city) %>%
addCircleMarkers(data = ru1, radius = 2,
color = ~pal(capital),
label = ~paste0(city, " (", capital, ")"))
# Add a legend that displays the colors used in pal
%>%
map3 addLegend(pal = pal,
values = c("primary", "admin", "minor"))
map %>%
map3 <- clearMarkers() %>%
clearBounds()