A large part of being able to design sophisticated plots is having control over the “non-data elements” of the plot, such as the plot title and axis titles.You want to be able to format those and polish them for publication and presentation.
In this tutorial, I’ll show you how to add plot titles and axis titles to your chart. I’ll also show you how to format them. This will serve as an introduction to the general topic of formatting (i.e., theming) within ggplot2.
First, let’s load a dataset that we can work with. We’ll also quickly plot the data to display the data visually, prior to formatting.
df.china_co2 <- read.csv(url("https://vrzkj25a871bpq7t1ugcgmn9-wpengine.netdna-ssl.com/wp-content/uploads/2015/01/china_co2_1961_to_2010_data.txt")) ggplot(data=df.china_co2, aes(x=year,y=co2_emission_per_cap_qt)) + geom_line()
This is just very simple line chart. No formatting. Everything here just takes on default values.
Notice that there is no chart title and the axis titles are just the variable names. By default,
When you’re doing data exploration (and not at the stage where your charts need to be polished) the defaults are fine.
Actually, I recommend that you don’t bother with chart formatting while you’re doing data exploration (assuming that in this stage, you’re not showing it to executives, partners, customers, etc). When you’re just exploring your data, keep it simple.
But later, you’ll need to polish your charts. Formatting matters. Design matters. Like it or not, the human mind likes beautiful things. If you can learn to make beautiful charts – charts that appeal to the human mind – your analyses will be taken more seriously. (And if you’re really good at making beautiful visualizations, you will be much more visible to important people. Trust me on this.)
Ok, let’s go back to the code. Now that we’ve seen an unformatted chart (without any title or axis labels), let’s add some titles. We’ll add unformatted titles first and then format them.
How to add a chart title and axis titles
Adding a chart title and axis titles is relatively easy.
What you want to do[attr style=”width:200px”], function, notes
Add chart title,
Add axis titles,
You add a chart title with the
You add axis titles with the
ggplot(data=df.china_co2, aes(x=year, y=co2_emission_per_cap_qt)) + geom_line() + ggtitle("China C02 Emissions") + labs(x="Year",y="C02 Emissions")
Keep in mind though that the formatting of the plot title and axis titles is controlled outside of the
You control the title formatting using the
theme() function: formatting in ggplot2
In ggplot2, formatting of “non data elements” is performed with the
Think of the
Almost any “non-data element” you want to format can be changed using the
I’ll show you an example first, and then we’ll use that example as a case that we can break down to understand how the
How to format your chart title and axis titles
As the instructive example, I’ll show you how to format the plot title and axis titles that we added to our line chart.
Here we’ll call the
#------------------- # FORMATTED TITLES # - plot.title # - axis.title #------------------- ggplot(data=df.china_co2, aes(x=year, y=co2_emission_per_cap_qt)) + geom_line() + ggtitle("China C02 Emissions") + labs(x="Year",y="C02 Emissions") + theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=32, hjust=0)) + theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=22))
In the above code, we’ve called the
Theme element, What it refers to
And here’s what those “theme elements” actually refer to, visually.
The first argument of the
That’s the way that the
Now look closer, inside each call of the
But we’re specifying how we want to modify those theme elements using
That’s how the ggplot2 theme system works: you call the
In this case, the plot and axis titles are “text” elements, so we call
Here’s a breakdown of the “text” parameters we’re using within
parameter, what it modifies
A special note about “inheritance”
Notice that the naming-conventions of the theme elements are highly systematic. Both
Like almost everything in ggplot2, there’s a deep structure (Hadley Wickham did a fantastic job designing ggplot2). Once you understand the structure that’s built into the system, things get easier.
You’ll note that in the above code, we specified that we wanted to modify
This is a case of “inheritance.”
That said, we can modify
Here’s a quick example. We’ll modify the angle of our y-axis title by calling
ggplot(data=df.china_co2, aes(x=year, y=co2_emission_per_cap_qt)) + geom_line() + ggtitle("China C02 Emissions") + labs(x="Year",y="C02 Emissions") + theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=32, hjust=0)) + theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=22)) + theme(axis.title.y = element_text(angle=0))
Recap: How to format text elements (like titles)
So let’s take a quick step back.
Plots have “theme elements” which are just things that we want to format. We call the
To format “text elements” specifically, you:
- Inside your
theme()call, specify the plot element you want to format as your first argument
- Then set the appearance of that element by calling
- Inside of
element_text(), set individual parameters to set the appearance of the element you want to format
Final note on workflow and the reason to learn this way
Ok, you might be thinking that this is a little complicated. You might even be tempted to go back to Excel and just do your plots there.
Don’t. Really, the ggplot system has a little bit of a learning curve, but once you overcome that learning curve and learn things like the theme system, you’ll be much more capable of producing great plots.
Moreover, the ggplot2 theme system can dramatically speed up your workflow. In a future article, I’m going to explain how to leverage the ggplot2 theme system to rapidly format your charts and save you time.
If you’d like to know when that walkthrough is published, then sign up for the email list now. I’ll send all of my new material to you as an email subscriber, direct to your inbox, so you’ll never miss a thing.