-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscldataR_map.R
More file actions
81 lines (63 loc) · 2.35 KB
/
scldataR_map.R
File metadata and controls
81 lines (63 loc) · 2.35 KB
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
#' Get geojson map.
#' @param level Optional. (1, 2)
#' @param isoalpha3 Optional. countries (alpha-3 country code)
#' @return A data frame with selected indicators
#' @importFrom stringr str_c
#' @importFrom sf read_sf
#' @importFrom tidyr gather
#' @export
#' @examples
#' get_map(level='1',isoalpha3="ARG")
get_map <- function(level='All', isoalpha3='All'){
#argument "indicator" is missing, with no default
if (!requireNamespace("sf", quietly = TRUE)) {
stop(
"Package \"sf\" must be installed to use this function.",
call. = FALSE
)
}
urls <- iadburls()
url <- urls$geojson_url
if(level!='All') url <- str_c(url,"&level=",level)
if(isoalpha3!='All') url <- str_c(url,"&isoalpha3=",isoalpha3)
map <- read_sf(url)
return(map)
}
#' Choropleth map.
#' @param year ISO 8601 string
#' @param level Optional. (1, 2)
#' @param isoalpha3 Optional. countries (alpha-3 country code)
#' @return A data frame with selected indicators
#' @importFrom dplyr left_join
#' @importFrom stringr str_c
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 geom_sf
#' @importFrom ggplot2 ggplot aes scale_fill_distiller labs theme element_blank
#' @importFrom scales pretty_breaks
#' @export
#' @examples
#' get_map(level='1',isoalpha3="ARG")
idbsocial_choropleth <- function(indicator, year, level='All', isoalpha3='All'){
if (!requireNamespace("sf", quietly = TRUE)) {
stop(
"Package \"sf\" must be installed to use this function.",
call. = FALSE
)
}
map <- get_map(level=level, isoalpha3=isoalpha3)
data <- query_indicator(indicator,countries=isoalpha3,year=toString(year))
output <- map %>% left_join(data,by ='isoalpha3')
map <- get_map(level=level, isoalpha3=isoalpha3)
data <- query_indicator(indicator,countries=isoalpha3,year=toString(year))
output <- map %>% left_join(data,by ='isoalpha3')
yy <- ggplot(data = output, aes(fill = value)) +
geom_sf(size = 0.25) +
scale_fill_distiller(name=indicator, palette = "Blues", breaks = pretty_breaks(), direction=1)+
labs(title = unique(output$label_es[!is.na(output$label_es)]),
caption = unique(output$label_es[!is.na(output$description_es)])) +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "white", color = NA))
return(yy)
}