The goal of the project is to Create a web page using R Markdown that features a map created with Leaflet. The project result will be hosted on either GitHub Pages, RPubs, or NeoCities.Your webpage must contain the date that you created the document, and it must contain a map created with Leaflet.
The rubric contains the following two questions:
First step is to load the necessary libraries
library(leaflet)
library(data.table)
library(dplyr)
For this project we will map the population of the largest cities in Norway. The data for the project is already downloaded and will be loaded into the global environment.
no_pop <- read.csv("norway_populations.csv", header = TRUE)
head(no_pop)
## city lat lng country iso2 admin_name capital population
## 1 Oslo 59.9133 10.7389 Norway NO Oslo primary 1064235
## 2 Bergen 60.3894 5.3300 Norway NO Vestland admin 291189
## 3 Stavanger 58.9700 5.7314 Norway NO Rogaland admin 237369
## 4 Sandnes 58.8517 5.7361 Norway NO Rogaland minor 237369
## 5 Trondheim 63.4297 10.3933 Norway NO Trøndelag minor 212660
## 6 Kristiansand 58.1472 7.9972 Norway NO Agder minor 125000
names(no_pop)
## [1] "city" "lat" "lng" "country" "iso2"
## [6] "admin_name" "capital" "population"
The table is loaded, but includes several columns we do not need for creation of the map. The next step is to select the required columns and create a smaller data frame for our leaflet output
pop_latlong <- no_pop %>% select(c("city", "lat", "lng", "population"))
head(pop_latlong)
## city lat lng population
## 1 Oslo 59.9133 10.7389 1064235
## 2 Bergen 60.3894 5.3300 291189
## 3 Stavanger 58.9700 5.7314 237369
## 4 Sandnes 58.8517 5.7361 237369
## 5 Trondheim 63.4297 10.3933 212660
## 6 Kristiansand 58.1472 7.9972 125000
df <- data.frame(pop_latlong)
The final step is to generate the map of populations in Norway
df %>%
leaflet() %>%
addTiles() %>%
addCircles(weight = 1, radius = sqrt(df$population)*100)