4.2 C
London
Friday, December 20, 2024
HomeTidyverse in Rggplot2 in RHow to Change Title Position in ggplot2 (With Examples)

How to Change Title Position in ggplot2 (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

By default, the title of plots in ggplot2 are left-aligned.

However, you can use the following methods to change the title position:

Method 1: Center the Title

some_ggplot +
  theme(plot.title = element_text(hjust = 0.5))

Method 2: Right-Align the Title

some_ggplot +
  theme(plot.title = element_text(hjust = 1))

Method 3: Adjust Title Position Vertically

some_ggplot +
  theme(plot.title = element_text(vjust = 10))

The following examples show how to use each method in practice with the built-in mtcars dataset in R.

Example 1: Center the Title

The following code shows how to create a scatterplot in ggplot2 and center the title using the hjust argument:

library(ggplot2)

#create scatterplot with centered title
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point() +
  ggtitle("Plot Title") +
  theme(plot.title = element_text(hjust = 0.5))

center title in ggplot2

Notice that the title is aligned in the center.

Example 2: Right-Align the Title

The following code shows how to create a scatterplot in ggplot2 and right-align the title using the hjust argument:

library(ggplot2)

#create scatterplot with right-aligned title
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point() +
  ggtitle("Plot Title") +
  theme(plot.title = element_text(hjust = 1))

ggplot2 right-align title

Notice that the title is aligned to the right.

Example 3: Adjust Title Position Vertically

The following code shows how to create a scatterplot in ggplot2 and move the title higher up using the vjust argument:

library(ggplot2)

#create scatterplot with title moved higher up
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point() +
  ggtitle("Plot Title") +
  theme(plot.title = element_text(hjust = 1, vjust = 3))

Notice that the title is moved higher up.

You can also provide a negative value to the vjust argument to move the title lower down:

library(ggplot2)

#create scatterplot with title moved down
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point() +
  ggtitle("Plot Title") +
  theme(plot.title = element_text(hjust = 1, vjust = -10))

Notice that the title is now moved inside the plot.

Additional Resources

The following tutorials explain how to perform other common operations in ggplot2:

How to Change the Legend Title in ggplot2
How to Rotate Axis Labels in ggplot2
How to Fix in R: could not find function “ggplot”

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories