Created
June 2, 2016 12:11
-
-
Save jonocarroll/96d1dd879b535c3c7ffe8f74065d4bc4 to your computer and use it in GitHub Desktop.
GDP per capita. Used as an example for add_images_as_xlabels which does what it suggests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rvest) | |
## GDP per capita, top 10 countries | |
url <- "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)_per_capita" | |
html <- read_html(url) | |
gdppc <- html_table(html_nodes(html, "table")[3])[[1]][1:10,] | |
## clean up; remove non-ASCII and perform type conversions | |
gdppc$Country <- gsub("Â ", "", gdppc$Country) | |
gdppc$Rank <- iconv(gdppc$Rank, "latin1", "ASCII", sub="") | |
gdppc$Country <- iconv(gdppc$Country, "latin1", "ASCII", sub="") | |
gdppc$`US$` <- as.integer(sub(",", "", gdppc$`US$`)) | |
## flag images (yes, this processing could be done neater, I'm sure) | |
## get the 200px versions | |
flags_img <- html_nodes(html_nodes(html, "table")[3][[1]], "img")[1:10] | |
flags_url <- paste0('http://', sub('[0-9]*px', '200px', sub('\\".*$', '', sub('^.*src=\\"//', '', flags_img)))) | |
flags_name <- sub('.*(Flag_of)', '\\1', flags_url) | |
if(!dir.exists("flags")) dir.create("flags") | |
for(flag in seq_along(flags_url)) { | |
switch(Sys.info()[['sysname']], | |
Windows= {download.file(flags_url[flag], destfile=file.path("flags", paste0(flag,"_", flags_name[flag])), method="auto", mode="wb")}, | |
Linux = {download.file(flags_url[flag], destfile=file.path("flags", paste0(flag,"_", flags_name[flag])))}, | |
Darwin = {print("Not tested on Mac. Use one of the above and find out?")}) | |
} | |
library(EBImage) ## readImage | |
library(dplyr) ## %>% | |
library(ggplot2) ## devtools::install_github("hadley/ggplot2) | |
library(grid) ## rasterGrob | |
library(ggthemes) ## theme_minimal | |
library(scales) ## comma | |
## create a dummy dataset | |
npoints <- length(flags_name) | |
y <- gdppc$`US$` | |
x <- seq(npoints) | |
dat <- data.frame(x=factor(x), y=y) | |
## load the images from filenames | |
## one day I'll remember to make these sorted on save | |
pics <- vector(mode="list", length=npoints) | |
image.file <- dir("flags", full.names=TRUE) | |
image.file <- image.file[order(as.integer(sub("_.*", "", sub("flags/", "", image.file))))] | |
## save the images into a list | |
for(i in 1:npoints) { | |
pics[[i]] <- EBImage::readImage(image.file[i]) | |
} | |
## create the graph, as per normal | |
## NB: #85bb65 is the color of money in the USA apparently. | |
gg <- ggplot(dat, aes(x=x, y=y/1e3L, group=1)) | |
gg <- gg + geom_bar(col="black", fill="#85bb65", stat="identity") | |
gg <- gg + scale_x_discrete() | |
gg <- gg + theme_minimal() | |
gg <- gg + theme(plot.margin = unit(c(0.5,0.5,5,0.5), "lines"), | |
axis.text.x = element_blank(), | |
axis.text.y = element_text(size=14)) | |
gg <- gg + scale_fill_discrete(guide=FALSE) | |
gg <- gg + theme(plot.background = element_rect(fill="grey90")) | |
gg <- gg + labs(title="GDP per Capita", subtitle=paste0("Top 10 countries\n(", url, ")"), x="", y="$US/1000") | |
gg | |
## insert imags (pics) as x-axis labels | |
## well, at least appear to do so | |
gg %>% add_images_as_xlabels(pics) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment