How to format your chart and axis titles in ggplot2

[This article was first published on SHARP SIGHT LABS » r-bloggers, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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()

 
format-chart-titles-axes-ggplot2

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, ggplot() simply uses your variable names as the axis titles.

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 ggtitle()
Add axis titles labs() the "x=" and "y=" parameters control the x-axis title and y-axis title respectively

You add a chart title with the ggtitle() function.

You add axis titles with the labs() function. Note that the labs() function has an “x=” parameter and a “y=” parameter. Those do exactly what they look like: they add the x-axis title and y-axis title, respectively.

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 ggtitle() and labs() functions.

You control the title formatting using the theme() function.

The theme() function: formatting in ggplot2

In ggplot2, formatting of “non data elements” is performed with the theme() function.

Think of the theme() function as a formatting system. It doesn’t change the data or the geometric objects of the plot. Rather, it changes the “look” and “style” of the plot.

Almost any “non-data element” you want to format can be changed using the theme() function. You just need to call the function by specifying which plot element you want to modify.

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 theme() function works.

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 theme() function to format our plot and axis titles.

#-------------------
# 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)) 

format-chart-titles-axes-ggplot2_FORMATTED-TITLES

In the above code, we’ve called the theme() function twice: once for plot.title and once for axis.title.

plot.title and axis.title are “theme elements.” (Notice in the below table that you can modify the x and y axes individually.)

Theme element What it refers to
plot.title the title of the plot
axis.title both axis titles
axis.title.x the x-axis title
axis.title.y the y-axis title

And here’s what those “theme elements” actually refer to, visually.

plot_title-labels_theme_elements_650x450

The first argument of the theme() function is the theme element that you want to format.

That’s the way that the theme() function works. You call it and specify the theme element of the plot that you want to modify.

Now look closer, inside each call of the theme() function. As I just noted, the first argument of the theme() function is the “theme element” that we want to modify.

But we’re specifying how we want to modify those theme elements using element_text(). Obviously, the axis title and plot titles are both “text.” When we modify “text” elements, we use the element_text() function to format them.

element_text() is an “element function” and it is used to format text elements.

That’s how the ggp

The post How to format your chart and axis titles in ggplot2 appeared first on SHARP SIGHT LABS.

To leave a comment for the author, please follow the link and comment on their blog: SHARP SIGHT LABS » r-bloggers.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)