Ниточки к данным, инфографике, оценкам
Автор - Андрей Гнидченко (ЦМАКП)
P. S. Рекламы нет
Post #107
709

Милота
ЗА @econbox
Showing posts older than #108 · Back to latest

Forwarded from FRAT - Financial random academic thoughts

library("rvest")
library("countrycode")
library("data.table")
library("ggplot2")
yr="2023"
mon="October"
url_cntrs <- paste0("https://www.imf.org/en/Publications/WEO/weo-database/",yr,"/",mon,"/select-countries?grp=2001&sg=All%20countries")
site_cntr <- read_html(url_cntrs)
Name_IMF <- site_cntr %>% html_nodes("[class='p-icon p-toggle p-plain']") %>% html_element("label") %>% html_text()
Code_IMF <- site_cntr %>% html_nodes("[class='weo-check']") %>% html_attr("value")
Curr_IMF <- site_cntr %>% html_nodes("[class='tooltip fade']") %>% html_attr("data-title")
Curr_IMF <- do.call(rbind,strsplit(Curr_IMF,"\n"))
Curr_IMF <- gsub("Primary domestic currency: ","",Curr_IMF[,1])
DATA <- as.data.frame(cbind(Name_IMF,Code_IMF,Curr_IMF))
DATA$Vers_WEO <- paste(yr, mon, sep="_")
DATA$Code_ISO3 <- countrycode(as.numeric(DATA$Code_IMF),'imf','iso3c')
DATA$Code_ISO2 <- countrycode(as.numeric(DATA$Code_IMF),'imf','iso2c')
DATA <- subset(DATA, is.na(DATA$Code_ISO3)==F & is.na(DATA$Code_ISO3)==F)
url_un <- "https://comtradeapi.un.org/files/v1/app/reference/Reporters.json"
y <- rjson::fromJSON(file=url_un)
rr <- lapply(y[1]$results, function(x) do.call("cbind",x))
ll <- lapply(y[1]$results, function(x) length(x))
un_cntrs_8 <- as.data.frame(do.call("rbind",rr[ll==8]))
un_cntrs_8[un_cntrs_8$reporterCode=="490",5] <- "TW"
un_cntrs_8[un_cntrs_8$reporterCode=="490",6] <- "TWN"
un_cntrs_actual <- as.data.frame(do.call("rbind",rr[ll==9]))
un_cntrs_retro1 <- as.data.frame(do.call("rbind",rr[ll==10]))
un_cntrs_retro2 <- subset(un_cntrs_actual, nchar(un_cntrs_actual$reporterCodeIsoAlpha2)>2)
un_cntrs_actual <- subset(un_cntrs_actual, nchar(un_cntrs_actual$reporterCodeIsoAlpha2)==2)
names(un_cntrs_retro2)[7:8] <- names(un_cntrs_retro1)[8:9]
full <- dplyr::bind_rows(un_cntrs_actual, un_cntrs_8, un_cntrs_retro1, un_cntrs_retro2)
DATA$Code_UN <- full$reporterCode[match(DATA$Code_ISO3, full$reporterCodeIsoAlpha3)]
url_cap <- "https://raw.githubusercontent.com/reganjohn/BHI/master/topology/country-capitals.csv"
cap <- fread(url_cap, na.strings="NULL")
cap <- subset(cap, is.na(cap$countryCode)==F)
DATA$continent <- cap$continent[match(DATA$Code_ISO2, cap$countryCode)]
DATA$capital <- cap$capital[match(DATA$Code_ISO2, cap$countryCode)]
DATA$lat_capital <- cap$latitude[match(DATA$Code_ISO2, cap$countryCode)]
DATA$lon_capital <- cap$longitude[match(DATA$Code_ISO2, cap$countryCode)]
url_coord <- "https://developers.google.com/public-data/docs/canonical/countries_csv"
site_coord <- read_html(url_coord)
crds <- site_coord %>% html_nodes("[class='devsite-article-body clearfix
']") %>% html_table()
crds <- crds[[1]]; crds$country[crds$name=="Namibia"] <- "NA"
DATA$lat_center <- crds$latitude[match(DATA$Code_ISO2, crds$country)]
DATA$lon_center <- crds$longitude[match(DATA$Code_ISO2, crds$country)]
DATA$lat_center[is.na(DATA$lat_center)==T] <- DATA$lat_capital[is.na(DATA$lat_center)==T]
DATA$lon_center[is.na(DATA$lon_center)==T] <- DATA$lon_capital[is.na(DATA$lon_center)==T]
world <- ggplot2::map_data("world")
mp <- ggplot(DATA) + geom_map(data = world, map = world, aes(map_id = region), fill=alpha("palegreen1",0.33), col="grey") +
geom_point(aes(lon_capital, lat_capital), col="sienna", size=0.7) + geom_point(aes(lon_center, lat_center), col="palegreen4", size=0.5) +
theme_void() + scale_y_continuous(limits=c(-50,80))
png(paste0(getwd(), '/mp_coord',substr(Sys.Date(),3,10),'.png'),
width = 8, height = 6, units = 'in', res = 700)
mp
dev.off()
