Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Code for visualizations in blog post http://szychtawdanych.pl/?p=1704
library(rvest)
library(ggplot2)
library(ggthemes)
library(highcharter)
avg_temp <- read_html('https://en.wikipedia.org/wiki/List_of_countries_by_average_yearly_temperature')
avg_temp <- html_table(avg_temp, fill = T)[[2]]
gdp <- read_html('https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(PPP)_per_capita')
gdp_tables <- html_table(gdp, fill = T)
gdp <- gdp_tables[[3]]
hdi <- read_html('https://en.wikipedia.org/wiki/List_of_countries_by_inequality-adjusted_HDI')
hdi_tables <- html_table(hdi, fill = T)
hdi <- bind_rows(hdi_tables[[3]], hdi_tables[[4]])
names(avg_temp)[3] <- "foo"
avg_temp <- avg_temp %>%
filter(is.na(foo)) %>%
select(country = Country,
temperature = "Average yearly temperature (1961-1990, Celsius)") %>%
mutate(temperature = as.numeric(temperature)) %>%
arrange(desc(temperature)) %>%
mutate(rank = row_number())
gdp <- gdp %>%
select(rank = Rank, country = "Country/Territory",
gdp_ppp = "Int$") %>%
mutate(country = if_else(country == "China[n 1]", "China", country)) %>%
rowwise %>%
mutate(gdp_ppp = as.numeric(gsub("[[:space:]]*,[[:space:]]*", "", gdp_ppp)))
plot_data <- inner_join(gdp, avg_temp, by = "country") %>%
inner_join(hdi, by = c("country"="Country"))
breaks_gdp <- c(0, 1e4, 2e4, 4e4,8e4, 16e4)
plot_data <- plot_data %>%
mutate(gdp_label = cut(gdp_ppp, breaks_gdp, labels = c("< 10 tys.$", "10 - 20 tys.$",
"20 - 40 tys.$",
"40 - 80 tys.$",
"80 - 120 tys.$")))
ggplot(plot_data, aes(x = temperature, y = IHDI, color = gdp_label)) +
geom_point() +
theme_fivethirtyeight(base_size = 20, base_family = "Helvetica Neue Light") +
labs(title = "Wpływ temperatury na rozwój państw",
x = "Przeciętna temperatura roczna",
y = "Human Development Index") +
guides(color = guide_legend(title = "PKB per capita", override.aes = aes(size = 6))) +
theme(axis.title = element_text(inherit.blank = F),
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "white"),
plot.background = element_rect(fill = "white"),
legend.background = element_rect(fill = "white"),
legend.key = element_rect(fill = "white")) +
ggplot2::annotate("text", x = 0, y = 0.3, label = "Korelacja: -0.7",
hjust=0, vjust=0, col="black", cex=6,
fontface = "bold", alpha = 0.5)
hchart(plot_data, "scatter", hcaes(x = temperature, y = IHDI,
label = country, group = gdp_label)) %>%
hc_title(text = "Przeciętna temperatura i rozwój krajów",
align = "center",
style = list(useHTML = TRUE)) %>%
hc_xAxis(title = list(text = "Przeciętna temperatura")) %>%
hc_yAxis(title = list(text = "Human Development Index")) %>%
hc_add_theme(hc_theme_538()) %>%
hc_chart(zoomType = "xy") %>%
hc_tooltip(useHTML = TRUE,
headerFormat = "<table>",
pointFormat = paste("<tr><th colspan=\"1\"><b>{point.label}</b></th></tr>",
"<tr><th>Przeciętna temperatura</th><td>{point.x} C</td></tr>",
"<tr><th>Human Development Index</th><td>{point.y} </td></tr>"),
footerFormat = "</table>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment