Faceting by country

1. Faceting by country

In the last exercise you learned to plot multiple countries, distinguishing them by color. This is great for two or three countries,

2. Graphing many countries

but consider this graph where the trends of six countries are compared to each other. I don’t know about you, but I find this hard to interpret- the overlapping lines are difficult to distinguish, and I find myself forgetting which color represents which country. Instead, let’s introduce an alternative approach: faceting,

3. Graphing many countries

or creating “sub-plots” for each country. To facet, add an additional option with + to the end of the plot: facet_wrap. Here you’ll use a tilde country: in R the tilde means “explained by”, which says that we want to divide the graph into one subplot by country. When the six countries are divided onto separate subplots, it becomes a lot easier to understand each country's trend. You might notice that all six graphs have the same y-axis, even though they cover different ranges. This leads to “wasted space” within each graph, where the trend in particular countries is compressed because of the patterns in other countries.

4. Graphing on separate scales

To avoid this, you can add a second argument, scales = “free_y”. This lets the y-axis vary between each subplot, and use all the space

5. Graphing on separate scales

it has available

6. Graphing on separate scales

There are advantages and disadvantages to this approach - while there’s less wasted space within each subplot, it can also be misleading while comparing between them- but it’s an option worth being aware of. Faceting is a powerful tool, and in the exercises you’ll see that it is capable of plotting and comparing even a large number of countries.

7. Let's practice!