Skip to content

Instantly share code, notes, and snippets.

@tmasjc
Created February 8, 2018 12:02
Show Gist options
  • Save tmasjc/a5e87fe95e9cc5c6a3f2d6dbee17d599 to your computer and use it in GitHub Desktop.
Save tmasjc/a5e87fe95e9cc5c6a3f2d6dbee17d599 to your computer and use it in GitHub Desktop.
Animate foreign exchange rate x outbound Chinese tourisism. #rstats #plotly
library(readxl)
library(tidyr)
library(ggplot2)
library(broom)
library(rvest)
library(plotly)
# Import data -------------------------------------------------------------
# read data
cot <- read_excel("~/Downloads/outbound_tourism_china.xlsx", skip = 5)
# mod columns name
colnames(cot) <- c("country", "series", 1995:2016, "pct_chg_16_15")
# Convert to tidy format
long <- cot %>%
filter(series == "TFR") %>%
select(-pct_chg_16_15, -series) %>%
gather(year, arrivals, -country)
long
# Top 10 most popular country in 2016
top10_2016 <- long %>% filter(year == 2016) %>% top_n(wt = arrivals, n = 10) %>% pull(country)
top10_2016
long <- long %>% filter(country %in% top10_2016) %>%
# special treatment for Hong Kong
mutate(country = gsub(country, pattern = "\\,\\s?\\w*$", replacement = "") %>% toupper())
long$country %>% unique()
# Currency code refence ---------------------------------------------------
# Get currency codes from iban.com
xml <- read_html("https://www.iban.com/currency-codes.html")
# Download table and convert to tibble
codes <- xml %>% html_node(css = "table.ibanindex.tablesorter.currencycodes") %>% html_table()
colnames(codes) <- c("country_name", "currency_name", "currency_letter", "currency_code")
codes <- codes %>%
mutate(country_name = gsub(country_name, pattern = "\\s+\\(THE\\)", replacement = "")) %>%
as.tibble()
codes
# Custom function to return year average ----------------------------------
base = "http://www.x-rates.com/average/?amount=100&from=CNY"
get_year_average <- function(to_country, year){
xml <- paste0(base, "&to=", to_country, "&year=", year) %>% read_html()
avg <- xml %>% html_nodes(css = ".avgRate") %>% html_text() %>% as.numeric() %>% mean()
if(!is.nan(avg)){
avg
}else{
NA
}
}
# Only keep interested country
codes <- codes %>% filter(country_name %in% long$country)
# Get codes
long <- long %>% right_join(codes %>% select(country_name, currency_letter), by = c("country" = "country_name"))
# Get yearly average
long <- long %>% rowwise() %>%
filter(year >= 2008) %>%
mutate(avg = get_year_average(currency_letter, year))
# Cambodia KHR is not available from xrates.com
long <- long %>% filter(!is.na(avg))
# Add currency growth
long <- long %>%
ungroup() %>%
mutate(avg_g = (avg - lag(avg)) / lag(avg) + 1)
# Animation ---------------------------------------------------------------
t <- list(
family = "sans serif",
size = 11,
color = toRGB("grey50"))
long %>%
filter(year > 2008) %>%
plot_ly(
x = ~avg_g,
y = ~arrivals,
text = ~country,
color = ~country,
frame = ~year,
type = "scatter",
mode = "markers",
marker = list(size = 15),
showlegend = F
) %>%
add_text(textfont = t, textposition = "top right") %>%
layout(
xaxis = list(
range = c(0.5, 1.5),
title = "CNY to Foreign Ex Ratio (Growth)"
),
yaxis = list(
type = "log",
title = "Number of Arrivals (log)"
)
) %>%
animation_opts(
1000, easing = "elastic", redraw = F
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment