Created
October 24, 2020 16:02
-
-
Save addiversitas/4d11d4828df3cab14151bc518709f903 to your computer and use it in GitHub Desktop.
basic data setup for sample covid chart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(leaflet) | |
library(rgdal) | |
library(RColorBrewer) | |
#load spatial data | |
world_spdf <- readOGR( | |
dsn = getwd() , | |
layer = "TM_WORLD_BORDERS_SIMPL-0.3", | |
verbose = FALSE | |
) | |
#load covid data set | |
covidData <- read.csv("https://covid19.who.int/WHO-COVID-19-global-data.csv", fileEncoding="UTF-8-BOM", stringsAsFactors = FALSE) | |
covidData <- na.omit(covidData) | |
#select a certain date | |
selectedData <- covidData[covidData$Date_reported == "2020-07-15", ] | |
#match cases and spatial data via ISO2/Country Code | |
world_spdf$Cases <- selectedData$Cumulative_cases[match(world_spdf$ISO2, selectedData$Country_code)] | |
#create label texts | |
world_spdf@data$LabelText <- paste0( | |
"<b>Country:</b> ", world_spdf@data$NAME,"<br>", | |
"<b>Cases:</b> ", format(world_spdf@data$Cases, nsmall=0, big.mark=",")) | |
#define colorpalette for chart legend | |
paletteBins <- c(0, 50000, 100000, 250000, 500000, 1000000, 2500000, 5000000, 10000000) | |
colorPalette <- colorBin(palette = "YlOrBr", domain = covidData$Cumulative_cases, na.color = "transparent", bins = paletteBins) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment