-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdg_plotter.R
110 lines (88 loc) · 3.22 KB
/
sdg_plotter.R
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
library(janitor)
library(dplyr)
library(sf)
library(readr)
glob_inds <- readr::read_csv("data/global_indicators.csv")
world <-
janitor::clean_names(rnaturalearth::ne_countries(scale = "medium", returnclass = "sf"))
world$iso_a3[world$admin == "France"] <- "FRA"
world$iso_a3[world$admin == "Indian Ocean Territory"] <- "IOT"
world$iso_a3[world$admin == "Kosovo"] <- "RKS"
world$iso_a3[world$admin == "Norway"] <- "NOR"
world_sf <- world %>%
dplyr::select(iso_a3, region_un, continent, pop_est, gdp_md_est) %>%
dplyr::mutate(code = iso_a3, region_un = as.factor(region_un)) %>%
left_join(., glob_inds, by = "code")
sdg_plotter <- function(goal_view = 1:17, var_in = "num_indicators"){
if(var_in == "num_indicators"){
num_ind_countries <- world_sf %>%
dplyr::filter(goal %in% {{goal_view}} &
region_un != "Antarctica" &
region_un != "Seven seas (open ocean)") %>%
dplyr::mutate(total_inds = n_distinct(indicator)) %>%
dplyr::group_by(entity) %>%
dplyr::summarise(sum_ind = sum(!is.na(latest_data_year)), prop_ind = 100*(sum_ind/unique(total_inds))) %>%
mutate(disp_val = replace(prop_ind, prop_ind == 0, NA))
bins <- c(1,seq(20,100, 20))
labels <- sprintf(
"<strong>%s</strong><br/>%g indicators",
num_ind_countries$entity,
num_ind_countries$sum_ind
) %>% lapply(htmltools::HTML)
map_out <- sf::as_Spatial(num_ind_countries)
}
if(var_in == "mean_age"){
mean_yr_countries <- world_sf %>%
dplyr::filter(goal %in% {{goal_view}} &
region_un != "Antarctica" &
region_un != "Seven seas (open ocean)") %>%
filter(!is.na(latest_data_year)) %>%
group_by(entity) %>%
summarise(mean_year = mean(latest_data_year), disp_val = mean_year)
if (min(mean_yr_countries$disp_val) < 2014){
bins <- round(seq(min(mean_yr_countries$disp_val),2020, length.out = 6))
}else{
bins <- seq(min(mean_yr_countries$disp_val),2020)
}
labels <- sprintf(
"<strong>%s</strong><br/>Average year of data availability: %g",
mean_yr_countries$entity,
mean_yr_countries$disp_val
) %>% lapply(htmltools::HTML)
map_out <- sf::as_Spatial(mean_yr_countries)
}
pal <-
leaflet::colorBin("YlOrRd", domain = map_out$disp_val, bins = bins)
leaflet::leaflet(map_out) %>%
setView(0, 30, 2) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
fillColor = ~ pal(disp_val),
weight = 1,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE
),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto"
)
) %>%
addLegend(
pal = pal,
values = ~ disp_val,
opacity = 0.7,
title = ifelse(var_in == "num_indicators","% Indicators\n with Data", "Date of Most Recent Data"), #should change to switch
position = "bottomleft",
labFormat = labelFormat(big.mark = "")
)
}