To add a title, we include the option labs and include the name of the graph as a string argument, and to change the axis names we use the labs command too.
p4 = p4 +
labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "Percentage") +
scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) p4
81%
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Year
Percentage
Copper Pulp wood, Fruit, Salmon & Others Source: The Observatory of Economic Complexity
Composition of Exports to China ($)
Adjusting color palette
To change the colours, we use the scale_colour_manual command. Note that you can reference the specific colours you’d like to use with specific HEX codes. You can also reference colours by name, with the full list of colours recognised by Rhere.
fill = c("#E1B378","#5F9EA0")
p4 = p4 + scale_fill_manual(values = fill) p4
81% 86% 80% 81% 85% 82% 80% 79% 78% 78%
0%
25%
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Year
Percentage
Copper Pulp wood, Fruit, Salmon & Others
Using the white theme
As explained in the previous posts, we can also change the overall look of the graph using themes. We’ll start using a simple theme customisation by adding theme_bw() after ggplot(). As you can see, we can further tweak the graph using the theme option, which we’ve used so far to change the legend.
p4 = ggplot(aes(y = percentage, x = year, fill = fct_rev(product)), data = charts.data) +
geom_col() +
geom_text(aes(label = paste0(percentage,"%")),
position = position_stack(vjust = 0.5), size = 4) + scale_x_continuous(breaks = seq(2006,2015,1)) +
scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) + labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "Percentage") +
theme_bw() +
theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank()) + guides(fill = guide_legend(reverse = T)) p4
81%
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Year
Percentage
Copper Pulp wood, Fruit, Salmon & Others Source: The Observatory of Economic Complexity
Composition of Exports to China ($)
Using system fonts
You can use fonts such as Tahoma within ggplot2. One option is the classic extrafont package but here we will use showtext package that allows a wider variety of options including Google Fonts. The reason behind this is that its easier to make it work under Windows, Ubuntu and OS X.
We’ll use XKCD, ‘Officina Sans’, ‘Atlas Grotesk’ and ‘Decima Mono Pro’ fonts. ‘Officina Sans’ is used in the plots that appear in The Economist while ‘Atlas Grotesk’ and ‘Decima Mono Pro’ are used by FiveThirtyEight.
The XKCD font that is available under an open licensehereorhere.
‘Officina Sans’ is a commercial font and is available here. However, as a replacement we use ‘Roboto Condensed’ provided freely by Google Fonts.
‘Atlas Grotesk’ and ‘Decima Mono Pro’ are commercial fonts and are availablehereandhere.
library(showtext)
p4 = ggplot(aes(y = percentage, x = year, fill = fct_rev(product)), data = charts.data) +
geom_col() +
geom_text(aes(label = paste0(percentage,"%")),
position = position_stack(vjust = 0.5), colour = "black", family = "XKCD", size = 5, show.legend = F) +
scale_x_continuous(breaks = seq(2006,2015,1)) +
scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) + labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "Percentage") +
scale_fill_manual(values = fill) +
theme(axis.line.x = element_line(size = .5, colour = "black"), axis.line.y = element_line(size = .5, colour = "black"), axis.text.x = element_text(colour = "black", size = 10), axis.text.y = element_text(colour = "black", size = 10), legend.key = element_rect(fill = "white", colour = "white"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(),
plot.title = element_text(family = "XKCD"), text = element_text(family = "XKCD")) + guides(fill = guide_legend(reverse = T)) p4
81%
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Year
Percentage
Copper Pulp wood, Fruit, Salmon & Others Source: The Observatory of Economic Complexity
Composition of Exports to China ($)
Using ‘The Economist’ theme
There are a wider range of pre-built themes available as part of the ggthemes package (more information on thesehere). Below we’ve applied theme_economist(), which approximates graphs in the Economist magazine. It is also important that the font change argument inside theme is optional and it’s only to obtain a more similar result compared to the original. For an exact result you need ‘Officina Sans’.
fill = c("#00a3dc","#01526d")
p4 = ggplot(aes(y = percentage, x = year, fill = fct_rev(product)), data = charts.data) +
geom_col() +
geom_text(aes(label = paste0(percentage,"%")),
position = position_stack(vjust = 0.5), colour = "white", family = "Roboto Condensed", size = 4) +
scale_x_continuous(breaks = seq(2006,2015,1)) +
scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) + labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "Percentage") +
theme_economist() + scale_fill_manual(values = fill) +
theme(axis.line.x = element_line(size = .5, colour = "black"),
81%
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 Year
Percentage
Copper Pulp wood, Fruit, Salmon & Others
Using ‘Five Thirty Eight’ theme
Below we’ve applied theme_fivethirtyeight(), which approximates graphs in the niceFiveThirtyEight website. Again, it is also important that the font change is optional and it’s only to obtain a more similar result compared to the original. For an exact result you need ‘Atlas Grotesk’ and ‘Decima Mono Pro’.
fill = c("#f80a1c","#338cd3")
p4 = ggplot(aes(y = percentage, x = year, fill = fct_rev(product)), data = charts.data) +
geom_col() +
geom_text(aes(label = paste0(percentage,"%")),
position = position_stack(vjust = 0.5), colour = "white",
family = "DecimaMonoPro-Bold", size = 4) + scale_x_continuous(breaks = seq(2006,2015,1)) +
scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) + labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "Percentage") +
theme_fivethirtyeight() + scale_fill_manual(values = fill) +
theme(axis.title = element_text(family = "Atlas Grotesk Regular"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(),
plot.title = element_text(family = "Atlas Grotesk Medium"), legend.text = element_text(family = "Atlas Grotesk Regular"), text = element_text(family = "Decima Mono Pro")) +
guides(fill = guide_legend(reverse = T)) p4
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Year
P e rc e nt a ge
Copper Pulp wood, Fruit, Salmon & Others Source: The Observatory of Economic Complexity
Composition of Exports to China ($)
Creating your own theme
As before, you can modify your plots a lot as ggplot2 allows many customisations. Here we present our original result shown at the top of page.
subtitle = "Source: The Observatory of Economic Complexity") + labs(x = "Year", y = "Percentage") +
scale_fill_manual(values = fill) +
theme(panel.border = element_rect(colour = "black", fill = NA, size = .5), axis.text.x = element_text(colour = "black", size = 10),
axis.text.y = element_text(colour = "black", size = 10), legend.key = element_rect(fill = "white", colour = "white"), legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank(),
panel.grid.major = element_line(colour = "#d3d3d3"), panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.title = element_text(size = 14, family = "Tahoma", face = "bold"), text = element_text(family = "Tahoma")) +
guides(fill = guide_legend(reverse = T)) p4
81%
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Year
Percentage
Copper Pulp wood, Fruit, Salmon & Others Source: The Observatory of Economic Complexity
Composition of Exports to China ($)
In this chapter, we will work towards creating the scatterplot below. We will take you from a basic scatterplot and explain all the customisations we add to the code step-by-step.
0 50 100 150
1 6 11 16 21 26 31