# Recreate Economist graph by ggplot2

##### Posted on March 03, 2017

In this blog we would use some of those techniques to reproduce a graphic from the Economist ( Most of the part of this blog has been taken from the Harvard Labs class of Introduction to R Graphics )

### Challenge: Recreate This Economist Graph

Graph source: http://www.economist.com/node/21541178

Building off of the graphics you created in the previous exercises, put the finishing touches to make it as close as possible to the original economist graph. ### Challenge Solution

Lets start by creating the basic scatter plot using Economist Data, then we can make a list of things that need to be added or changed. The basic plot loogs like this: To complete this graph we need to:

• change the point shape to open circle
• change the order and labels of Region
• label select points
• fix up the tick marks and labels
• move color legend to the top
• title, label axes, remove legend title
• theme the graph with no vertical guides
• final touches to make it perfect (use image editor for this)

Adding the trend line is not too difficult, though we need to guess at the model being displyed on the graph. A little bit of trial and error leds to Notice that we put the geom_line layer first so that it will be plotted underneath the points, as was done on the original graph

### Use open points

This one is a little tricky. We know that we can change the shape with the shape argument, what what value do we set shape to? The example shown in ?shape can help us: This shows us that shape 1 is an open circle, so That is better, but unfortunately the size of the line around the points is much narrower than on the original. This is a frustrating aspect of ggplot2, and we will have to hack around it. One way to do that is to multiple point layers of slightly different sizes. ### Labelling points

This one is tricky in a couple of ways. First, there is no attribute in the data that separates points that should be labelled from points that should not be. So the first step is to identify those points.

Now we can label these points using geom_text, like this: This more or less gets the information across, but the labels overlap in a most unpleasing fashion. We can use the ggrepel package to make things better, but if you want perfection you will probably have to do some hand-adjustment. ### Change the region labels and order

Thinkgs are starting to come together. There are just a couple more things we need to add, and then all that will be left are themeing changes.

Comparing our graph to the original we notice that the labels and order of the Regions in the color legend differ. To correct this we need to change both the labels and order of the Region variable. We can do this with the factor function.

Now when we construct the plot using these data the order should appear as it does in the original. Add title and format axes The next step is to add the title and format the axes. We do that using the scales system in ggplot2. ### Theme tweaks

Our graph is almost there. To finish up, we need to adjust some of the theme elements, and label the axes and legends. This part usually involves some trial and error as you figure out where things need to be positioned. To see what these various theme settings do you can change them and observe the results. ### Add model R2 and source note

The last bit of information that we want to have on the graph is the variance explained by the model represented by the trend line. Lets fit that model and pull out the R2 first, then think about how to get it onto the graph.

OK, now that we’ve calculated the values, let’s think about how to get them on the graph. ggplot2 has an annotate function, but this is not convenient for adding elements outside the plot area. The grid package has nice functions for doing this, so we’ll use those.

And here it is, our final version! Comparing it to the original suggests that we’ve got most of the important elements, though of course the two graphs are not identical. Comparing it to the original suggests that we’ve got most of the important elements, though of course the two graphs are not identical. 