How to format your chart and axis titles in ggplot2
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Once you know how to create simple plots you’ll want to learn how to design more sophisticated plots.
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("http://www.sharpsightlabs.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 | function | notes |
---|---|---|
Add chart title | |
|
Add axis titles | |
the "x=" and "y=" parameters control the x-axis title and y-axis title respectively |
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
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 |
---|---|
the title of the plot | |
both axis titles | |
the x-axis title | |
the y-axis title |
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 ggp
The post How to format your chart and axis titles in ggplot2 appeared first on SHARP SIGHT LABS.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.