How to Center Plot Title in ggplot2 with R

How To Center Plot Title In Ggplot2 With R

The easiest method to middle a title utilizing ggplot2 is to modify the plot.title part of the theme utilizing element_text(hjust=0.5). For instance,

library(ggplot2)
ggplot(knowledge=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon") +
     theme(plot.title=element_text(hjust=0.5))

This tutorial will undergo how to regulate the plot title utilizing ggplot2 with code examples.

Example

Consider the next scatterplot of two variables from the mtcars dataset miles per gallon (mpg) and weight (wt).

library(ggplot2)

# Create scatterplot utilizing ggplot2

ggplot(knowledge=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon")

By default, plot titles in ggplot2 are left-aligned.

Mtcars: Wt Vs Mpg Scatter Plot, Left-Aligned Title
Mtcars: Wt Vs Mpg Scatter Plot, Left-Aligned Title

Center Aligned utilizing hjust

We can regulate the plot theme utilizing plot.title and element_text. The operate element_text specifies the show of the textual content part of the plot.

The hjust argument for element_text shifts the plot title textual content horizontally. If we set hjust to 0.5, we centre the title. Let’s take a look at the revised code:

library(ggplot2)
ggplot(knowledge=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon") +
     theme(plot.title=element_text(hjust=0.5))
Mtcars: Wt Vs Mpg Scatter Plot, Centre-Aligned Title
Mtcars: Wt Vs Mpg Scatter Plot, Centre-Aligned Title

We can see that the title is now centre-algined.

Center Aligned utilizing hjust and vjust

We can shift the title horizontally and vertically utilizing the hjust and vjust arguments. If we set vjust to a adverse worth the title strikes down the plot, if we set it to a optimistic worth the title strikes up the plot. Let’s take a look at the revised code:

library(ggplot2)
ggplot(knowledge=mtcars, aes(x=mpg, y=wt)) + geom_point() + 
     ggtitle("Automobile Weight vs Miles per Gallon") +
     theme(plot.title=element_text(hjust=0.5, vjust=-7))
Mtcars: Wt Vs Mpg Scatter Plot, Centre-Aligned And Vertically Adjusted Title
Mtcars: Wt Vs Mpg Scatter Plot, Centre-Aligned And Vertically Adjusted Title

We can see that the title has moved down and is contained in the plot.

READ :  [Solved] pycallgraph Install Error: subprocess-exited-with-error

Right Aligned utilizing hjust and vjust

We can set right-align the title by setting hjust to 1. Let’s take a look at the revised code:

ggplot(knowledge=mtcars, aes(x=mpg, y=wt)) + geom_point() + 

     ggtitle("Automobile Weight vs Miles per Gallon") +

     theme(plot.title=element_text(hjust=1, vjust=-7))
Mtcars: Wt Vs Mpg Scatter Plot, Right-Aligned And Vertically Adjusted Title
Mtcars: Wt Vs Mpg Scatter Plot, Right-Aligned And Vertically Adjusted Title

We can see that the title is right-aligned and shifted down contained in the plot.

Summary

Congratulations on studying to the tip of this tutorial!

For additional studying on R, go to the articles:

Go to the on-line programs web page on R to study extra about coding in R for knowledge science and machine studying.

Have enjoyable and blissful researching!


Leave a Reply

Your email address will not be published. Required fields are marked *