Connecting R and Python. Why, when and where

Extended Example with R Notebook

TASK: Compare inflation rates in countries under inflation targeting monetary policy framework in the post-crisis period from 2013 - the beginning of the regime implementation in Russia.

Download data: Python

import pandas as pd
import wbdata as wd

# define a period of time
start_year = 2013
end_year = 2016

# list of countries under inflation targeting monetary policy regime
countries = ['AM', 'AU', 'AT', 'BE', 'BG', 'BR', 'CA', 'CH', 'CL', 'CO', 'CY', 'CZ', 'DE', 'DK', 'XC', 'ES', 'EE', 'FI', 'FR', 'GB', 'GR', 'HU', 'IN', 'IE', 'IS', 'IL', 'IT', 'JM', 'JP', 'KR', 'LK', 'LT', 'LU', 'LV', 'MA', 'MD', 'MX', 'MT', 'MY', 'NL', 'NO', 'NZ', 'PK', 'PE', 'PH', 'PL', 'PT', 'RO', 'RU', 'SG', 'SK', 'SI', 'SE', 'TH', 'TR', 'US', 'ZA']

# set dictionary for wbdata
inflation = {'FP.CPI.TOTL.ZG': 'CPI_annual', 'NY.GDP.MKTP.KD.ZG': 'GDP_annual'}

# download wb data
df = wd.get_dataframe(inflation, country = countries, data_date = (pd.datetime(start_year, 1, 1), pd.datetime(end_year, 1, 1)))

print(df.head())
df.to_csv('WB_data.csv', index = True)
                CPI_annual  GDP_annual
country   date                        
Armenia   2016   -1.271210    0.200000
          2015    3.726586    3.000000
          2014    2.980988    3.600000
          2013    5.793788    3.300000
Australia 2016    1.276991    2.765773

Process data: R

library(tidyverse)
library(data.table)
library(DT)
# get df with python results
cpi <- fread('WB_data.csv')
cpi <- cpi %>% group_by(country) %>% summarize(cpi_av = mean(CPI_annual), cpi_max = max(CPI_annual),
                                               cpi_min = min(CPI_annual), gdp_av = mean(GDP_annual)) %>% ungroup
cpi <- cpi %>% mutate(country = replace(country, country %in% c('Czech Republic', 'Korea, Rep.', 'Philippines',
                                                                'Russian Federation', 'Singapore', 'Switzerland',
                                                                'Thailand', 'United Kingdom', 'United States'),
                                        c('Czech', 'Korea', 'Phil', 'Russia', 'Singap', 'Switz', 'Thai', 'UK', 'US')),
                      gdp_sign = ifelse(gdp_av > 0, 'Positive', 'Negative'),
                      gdp_sign = factor(gdp_sign, levels = c('Positive', 'Negative')),
                      country = fct_reorder(country, gdp_av),
                      gdp_av = abs(gdp_av),
                      coord = rep(ceiling(max(cpi_max)) + 2, dim(cpi)[1])
                     )
print(head(data.frame(cpi)))

Visualization: R

library(viridis)
library(scales)
ggplot(cpi, aes(country, y = cpi_av)) +
  geom_linerange(aes(x = country, y = cpi_av, ymin = cpi_min, ymax = cpi_max, colour = cpi_av), size = 1.8, alpha = 0.9) + 
  geom_point(aes(x = country, y = coord, size = gdp_av, shape = gdp_sign), alpha = 0.5) +
  scale_size_area(max_size = 8) +
  scale_colour_viridis() +
  guides(size = guide_legend(title = 'Average annual\nGDP growth, %', title.theme = element_text(size = 7, angle = 0)),
         shape = guide_legend(title = 'Sign of\nGDP growth, %', title.theme = element_text(size = 7, angle = 0)),
         colour = guide_legend(title = 'Average\nannual CPI, %', title.theme = element_text(size = 7, angle = 0))) +
  ylim(floor(min(cpi$cpi_min)) - 2, ceiling(max(cpi$cpi_max)) + 2) +
  labs(title = 'Average Inflation and GDP Rates in Inflation Targeting Countries',
       subtitle = paste0('For the period 2013-2016'),
       x = NULL, y = NULL) +
  coord_polar() +
  theme_bw() +
  theme(legend.position = 'right',
        panel.border = element_blank(),
        axis.text.x = element_text(colour = '#442D25', size = 6, angle = 21, vjust = 1))
Ignoring unknown aesthetics: y

Done.

LS0tDQp0aXRsZTogIlIgTm90ZWJvb2siDQpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sNCi0tLQ0KDQojIENvbm5lY3RpbmcgUiBhbmQgUHl0aG9uLiBXaHksIHdoZW4gYW5kIHdoZXJlDQojIyBFeHRlbmRlZCBFeGFtcGxlIHdpdGggUiBOb3RlYm9vaw0KDQoqKlRBU0sqKjogQ29tcGFyZSBpbmZsYXRpb24gcmF0ZXMgaW4gY291bnRyaWVzIHVuZGVyIGluZmxhdGlvbiB0YXJnZXRpbmcgbW9uZXRhcnkgcG9saWN5IGZyYW1ld29yayBpbiB0aGUgcG9zdC1jcmlzaXMgcGVyaW9kIGZyb20gMjAxMyAtIHRoZSBiZWdpbm5pbmcgb2YgdGhlIHJlZ2ltZSBpbXBsZW1lbnRhdGlvbiBpbiBSdXNzaWEuDQoNCiMjIyBEb3dubG9hZCBkYXRhOiBQeXRob24NCg0KYGBge3B5dGhvbn0NCmltcG9ydCBwYW5kYXMgYXMgcGQNCmltcG9ydCB3YmRhdGEgYXMgd2QNCg0KIyBkZWZpbmUgYSBwZXJpb2Qgb2YgdGltZQ0Kc3RhcnRfeWVhciA9IDIwMTMNCmVuZF95ZWFyID0gMjAxNg0KDQojIGxpc3Qgb2YgY291bnRyaWVzIHVuZGVyIGluZmxhdGlvbiB0YXJnZXRpbmcgbW9uZXRhcnkgcG9saWN5IHJlZ2ltZQ0KY291bnRyaWVzID0gWydBTScsICdBVScsICdBVCcsICdCRScsICdCRycsICdCUicsICdDQScsICdDSCcsICdDTCcsICdDTycsICdDWScsICdDWicsICdERScsICdESycsICdYQycsICdFUycsICdFRScsICdGSScsICdGUicsICdHQicsICdHUicsICdIVScsICdJTicsICdJRScsICdJUycsICdJTCcsICdJVCcsICdKTScsICdKUCcsICdLUicsICdMSycsICdMVCcsICdMVScsICdMVicsICdNQScsICdNRCcsICdNWCcsICdNVCcsICdNWScsICdOTCcsICdOTycsICdOWicsICdQSycsICdQRScsICdQSCcsICdQTCcsICdQVCcsICdSTycsICdSVScsICdTRycsICdTSycsICdTSScsICdTRScsICdUSCcsICdUUicsICdVUycsICdaQSddDQoNCiMgc2V0IGRpY3Rpb25hcnkgZm9yIHdiZGF0YQ0KaW5mbGF0aW9uID0geydGUC5DUEkuVE9UTC5aRyc6ICdDUElfYW5udWFsJywgJ05ZLkdEUC5NS1RQLktELlpHJzogJ0dEUF9hbm51YWwnfQ0KDQojIGRvd25sb2FkIHdiIGRhdGENCmRmID0gd2QuZ2V0X2RhdGFmcmFtZShpbmZsYXRpb24s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