
R is the perfect language for data mining and scraping, especially when it comes to manipulating datasets that Excel just can’t handle. But, as important as the analysing and cleaning processes may be, visualising information is a key aspect when doing data-lead journalism!
Luckily for us, R includes a complete and very intuitive package for creating stunning graphics called ggplot2. It takes time and practise to master it, but with this step-by-step guide, we are sure you’ll be plotting and charting like a pro in no time.
In the examples, we’ll use the gapminder dataset, which contains countries’ information on population, GDP per capita, life expectancy and more. This dataset is often used on the datacamp exercises and works perfectly for visualising complex data.
Creating a scatter plot
First things first, install the following packages and load their libraries. For these exercises we’ll need: dplyr, ggplot2 and gapminder. Once you’ve done this, write gapminder in the script and run it to see what the dataset contains.
You’ll see our dataset contains 1,706 observations and 6 variables. Some of the information we have are country names, years, regions, and population. In this case, we will focus on the information related to the year 2007. So we’ll proceed to filter our data, like this:
gapminder_2007 <- gapminder %>%
filter(year == 2007)
Once we’ve reduced the data to the period of time we’re interested in, we’ll use the following code, that incorporates two varaibles: population per country and life expectancy, and the geom_point() feature, that’s specifically used for creating scatter plots.
ggplot(gapminder_2007, aes(x = pop, y = lifeExp)) +
geom_point() + scale_x_log10()

This should give us a plot like this: with Population on the x axis and Life Expectancy on the y axis. It doesn’t look bad, but it seems like there’s stuff missing, doesn’t it? Let’s try adding some colour to it to make it easier to read.
To do so, we’ll use the following code. Note that it is almost the same as the previous one and it barely changes a bit. In this case, we’ll specify we want to colour the dots according to the continent variable. You’ll have something like this:
ggplot(gapminder_2007, aes(x = pop, y = lifeExp, color = continent)) +
geom_point() + scale_x_log10()

It is easier to make an interpretation now, isn’t it? Another great feature of ggplot is that it allows us to resize the dots according to a specific variable. In this case, we’ll size them according to each country’s GDP per capita. Again, you just need to add a little less than a line of code.
ggplot(gapminder_2007, aes(x = pop, y = lifeExp, color = continent, size = gdpPercap)) + geom_point() + scale_x_log10()

We now have a scatter plot that shows us that the countries with higher life expectancy also have a bigger GDP per capita. Very useful when making an analysis of our data, isn’t it? But there’s still something important missing… the title of our graph! Fortunately, thanks to ggplot, it won’t take us more than a few lines of codes to do it.
my_graph <- ggplot(gapminder_2007, aes(x = pop, y = lifeExp, color = continent, size = gdpPercap)) + geom_point() + scale_x_log10()
print(my_graph + ggtitle("Life expectancy per continent in 2007"))
…And we’ll end up having something like this:

We can also use the facet feature to divide the life expectancy variable by country in separate charts. We only need to use facet_wrap(), like this:
ggplot(gapminder_2007, aes(x = pop, y = lifeExp, color = continent)) +
geom_point() + scale_x_log10() + facet_wrap(~ continent)

Visualising summarised data
In the previous examples, we worked with data from a specific year, but what if we want to visualise the change over time? Then we can use dplyr’s summarize() function and then make a graph with ggplot. In this case, we’ll focus on the GDP per capita.
by_year_continent <- gapminder %>%
group_by(continent, year) %>%
summarize(medianGdpPercap = median(gdpPercap))
After we’ve done our summary, we proceed to create a scatter plot with the ggplot package. Note that we added colour to differentiate the perfomance of every continent over time.
ggplot(by_year_continent, aes(x = year, y = medianGdpPercap, color = continent)) + geom_point() + expand_limits(y = 0)

Creating a line plot
Ggplot’s geom_line() function allows us to visualise data in a nice and tidy way. It also works great when we want to illustrate changes over time.
We’ll use the same summarised data from the previous exercise (by_year_continent) and change the code a bit.
ggplot(by_year_continent, aes(x = year, y = medianGdpPercap, color = continent)) + geom_line() + expand_limits(y = 0)
We'll end up with a nice and professional graph like this.

Creating bar plots
Bar plots are very clear and easy to read. To do this, we’ll use ggplot’s geom_col() feature and work with summarised data that contains the median GDP per capita per continent. As you can tell, we’ve used dplyr’s summarize() function again.
by_continent <- gapminder %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarize(medianGdpPercap = median(gdpPercap))
ggplot(by_continent, aes(x = continent, y = medianGdpPercap, color = continent, fill = continent)) +
geom_col()

Creating a histogram
Histograms are an approximate representation of the distribution of numerical data. These are useful when we’re in the data analysis phase and are also easy to create with ggplot’s geom_histrogram(). Contrary to the previous examples, to create a histogram we only need one variable.
gapminder_2007 <- gapminder %>%
filter(year == 2007)
ggplot(gapminder_2007, aes(x = pop)) +
geom_histogram() +
scale_x_log10()
