Recently I have been more interested to perform web scraping to extract public data to perform data analysis. There are probably many R packages out there that do a good job at such task, but I found out that the rvest was among the most popular ones. Hence I decided to give it a try.
In this post, I’m using the
rvest library to visualize how many medals were obtained by each country during the summer Olympics 2016. I thought it would be a first good exercise to get my hands around it.
Extracting data from web
First, let’s extract medals count information from the web. Here we are going to use data from NBC website.
Get country codes and tidy the data
We are now almost ready to plot the data. I thought it would be interesting to enhance the visualization by associating each country to its flag. To do so, I first need to extract the two-characters ISO code for each country using the countrycode package. This is pretty straightforward using the
countrycode() function. The next few lines are only used to tidy the data so it works well with
Plotting the data
ggplot2 it is easy to plot the data. Do not forget to install the
ggflags package from Github using
As simple as that!