Introduction

This document present the Peer-graded Assignment: R Markdown and Leaflet for the course “Developing Data Products” from Coursera.

The assignments requierments are the following:

For this assignment I will present on the map the main locations from Timis County, Romania (European Union). I searched on the web spatial data and found information about population by locations on TEMPO-Online databes, of the Romanian National Institute of Statistics. Since the database cannot be filtered automatically, I downloaded the data in excel.

The dataset contain major locations from Timis County: cities, towns and communes (the villages that are administratively assigned to communes are not presented separately).

Load Packages and Data

# load packages
library(readxl)
library(leaflet)

#load the dataset
fileName <- 'PopulationByLocationTimisCounty_2020.xlsx'
TimisCountyData <- read_excel(fileName)

head(TimisCountyData)
## # A tibble: 6 x 6
##   Siruta Location  Type  Latitude Longitude Population
##    <dbl> <chr>     <chr>    <dbl>     <dbl>      <dbl>
## 1 155243 TIMISOARA City      45.8      21.2     325363
## 2 155350 LUGOJ     City      45.7      21.9      46409
## 3 155403 BUZIAS    Town      45.6      21.6       8389
## 4 156357 CIACOVA   Town      45.5      21.1       5453
## 5 155458 DETA      Town      45.4      21.2       7638
## 6 156801 FAGET     Town      45.8      22.2       7602

Preparing Icons and Popups

#Icon Image with Romania flag colors
RomaniaFlag <- makeIcon(iconUrl="https://www.exportgenius.in/global-data/img-flg/romania.png",iconWidth = 16, iconHeight = 16,iconAnchorX = 0, iconAnchorY = 36)

#Preparing popups to show location name and its population numbar

TimisPopup<-rep(NA,nrow(TimisCountyData))
for(i in 1:nrow(TimisCountyData)) {
TimisPopup[i]<-paste("Location","=",TimisCountyData[i,2],"<br/>","Population","=",TimisCountyData[i,6])}

Adding Multiple markers with Popups to our created map widget

Will use the piping notation “%>%” to chain the stage output as input to next stage:

TimisCountyData %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers(icon = RomaniaFlag, popup = TimisPopup)
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively

Drawing circles with its radius to show location population and adding legend

TimisCountyData %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers(icon = RomaniaFlag, popup = TimisPopup) %>%
  addCircles(weight = 1, radius = sqrt(TimisCountyData$Population)*60,color = ifelse(TimisCountyData$Type=="City","blue", "red")) %>%
addLegend(labels = c("City","Town & Commune" ), colors = c("blue","red"))
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively