-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.Rmd
62 lines (48 loc) · 2 KB
/
test1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
title: ''
leafletmap: yes
output:
html_document:
df_print: paged
always_allow_html: yes
---
```{r data, message=FALSE,error=FALSE, warning=FALSE, echo=FALSE}
library(geojsonio)
library(leaflet)
library(leaflet.extras)
# you gave me a spreadsheet
library(readxl)
coronavirus_19 <- read_excel("~/Downloads/coronavirus 19th May.xlsx",
col_types = c("text", "numeric"))
# import the geojson as sp (spacial format - allows us to work with leaflet)
world_map <- geojson_read("https://opendata.arcgis.com/datasets/a21fdb46d23e4ef896f31475217cbb08_1.geojson", what = "sp")
# tigris has the handy geo_join
library(tigris)
library(dplyr)
# join our datasets - tell it the two files and then the two columns to join by
world_data <- geo_join(world_map, coronavirus_19, "CNTRY_NAME", "map_names")
#test a simple map to see if our shapes work
# use old-school R (base R) to get rid of the NAs (subset to give us anything not an NA)
world_data <- subset(world_data, !is.na(total.cases))
# testing a text popup
hovertext <- paste0("Total cases: ", as.character(world_data$total.cases), " in ",as.character(world_data$CNTRY_NAME))
# set the colour palette - google this you can do so much better
pal <- colorNumeric("Reds", domain = world_data$total.cases)
# test our map to see if it works
wolrdcase <-leaflet(options = leafletOptions(minZoom = 1,maxZoom = 4,dragging = TRUE)) %>%
addTiles() %>% clearBounds() %>%
addPolygons(data = world_data, fillColor = ~pal(world_data$total.cases),
fillOpacity = 0.8,
weight = 0.3,
smoothFactor = 0.2,
label = ~hovertext) %>% addSearchOSM()
mymap <- wolrdcase %>% addLegend(pal = pal,
opacity = 0.75,values = c(10000:300000),
title = "Total Cases",
position = "topleft")
library(htmlwidgets)
saveWidget(widget = mymap, file = "/Users/meloncheung/DanZhang-Exam/mymap.html")
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```