Site icon R-bloggers

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

 

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

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

You add axis titles with the < inline_code>labs() function. Note that the < inline_code>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 < inline_code>ggtitle() and < inline_code>labs() functions.

You control the title formatting using the < inline_code>theme() function.

The < inline_code>theme() function: formatting in ggplot2

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

Think of the < inline_code>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 < inline_code>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 < inline_code>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 < inline_code>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)) 

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

< inline_code>plot.title and < inline_code>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
< inline_code>plot.title the title of the plot
< inline_code>axis.title both axis titles
< inline_code>axis.title.x the x-axis title
< inline_code>axis.title.y the y-axis title

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

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

That’s the way that the < inline_code>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 < inline_code>theme() function. As I just noted, the first argument of the < inline_code>theme() function is the “theme element” that we want to modify.

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

< inline_code>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.