# загрузка пакетов
library('data.table')
library('WDI')
library('leaflet')
# https://cran.r-project.org/bin/windows/Rtools/
# devtools::install_github('mages/googleVis')
suppressPackageStartupMessages(library('googleVis'))

# для загрузки свежей версии pandoc:
#  https://github.com/pandoc-extras/pandoc-nightly/releases/tag/hash-7c20fab3
#  архив pandoc-windows-7c20fab3.zip распаковать в RStudio/bin/pandoc

Интерактивная картограмма

Интерактивная картограмма на данных Всемирного Банка по ВВП на душу населения в текущих ценах (долл. США) за 2019 год.

# данные по ВВП по ППП
indicator.code <- 'NY.GDP.PCAP.PP.CD'
DT <- data.table(WDI(indicator = indicator.code, start = 2019, end = 2019))

# все коды стран iso2
fileURL <- 'https://pkgstore.datahub.io/core/country-list/data_csv/data/d7c9d7cfb42cb69f4422dec222dbbaa8/data_csv.csv'
all.iso2.country.codes <- read.csv(fileURL, stringsAsFactors = F, 
                                   na.strings = '.')

# убираем макрорегионы
DT <- na.omit(DT[iso2c %in% all.iso2.country.codes$Code, ])

# объект: таблица исходных данных
g.tbl <- gvisTable(data = DT[, -'year'], 
                   options = list(width = 300, height = 400))
# объект: интерактивная карта
g.chart <- gvisGeoChart(data = DT, 
                        locationvar = 'iso2c', 
                        hovervar = 'country',
                        colorvar = indicator.code, 
                        options = list(width = 500, 
                                       height = 400, 
                                       dataMode = 'regions'))
# размещаем таблицу и карту на одной панели (слева направо)
TG <- gvisMerge(g.tbl, g.chart, 
                horizontal = TRUE, 
                tableOptions = 'bgcolor=\"#CCCCCC\" cellspacing=10')

# вставляем результат в html-документ
TG
MergedID12fc70e86f7b

Data: various • Chart ID: MergedID12fc70e86f7bgoogleVis-0.6.11
R version 4.0.4 (2021-02-15) • Google Terms of Use • Data Policy: See individual charts

Использованы пакеты R

  1. Matt Dowle and Arun Srinivasan (2021). data.table: Extension of data.frame. R package version 1.14.0. https://CRAN.R-project.org/package=data.table
  2. Vincent Arel-Bundock (2021). WDI: World Development Indicators and Other World Bank Data. R package version 2.7.4. https://CRAN.R-project.org/package=WDI
  3. Joe Cheng, Bhaskar Karambelkar and Yihui Xie (2021). leaflet: Create Interactive Web Maps with the JavaScript 'Leaflet' Library. R package version 2.0.4.1. https://CRAN.R-project.org/package=leaflet
  4. Markus Gesmann and Diego de Castillo. Using the Google Visualisation API with R. The R Journal, 3(2):40-44, December 2011.

Источники данных

  1. Dynamic Graphics with the googleVis Package http://rpubs.com/gallery/googleVis

  2. Дмитрий Храмов, «Сбор данных в Интернете на языке R». – М.: ДМК Пресс, 2017. – 280 с.

  3. Документация по API Геокодера от Яндекс https://tech.yandex.ru/maps/doc/geocoder/desc/concepts/about-docpage/

  4. Simon Munzert, Christian Rubba, Peter Meisner, Dominic Nyhuis Automated Data Collection with R. Wiley, 2015 https://books.google.ru/books?id=X-7sBQAAQBAJ&pg=PA96&lpg=PA96&dq=r+xml+xpath+returns+empty&source=bl&ots=OSnRWmFKTR&sig=ACfU3U2wE7shk3mFHrLrlMkFSfwvuAYpEg&hl=ru&sa=X&ved=2ahUKEwiu8_nVmrHhAhUMs4sKHe5vDFIQ6AEwCXoECAkQAQ#v=onepage&q=r%20xml%20xpath%20returns%20empty&f=false