Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Move Axis Labels in ggplot in R

  • Last Updated : 17 Jun, 2021

In this article, we are going to see how to move the axis labels using ggplot2 bar plot in the R programming language.

First, you need to install the ggplot2 package if it is not previously installed in R Studio. For creating a simple bar plot we will use the function geom_bar( ).

Syntax:

geom_bar(stat, fill, color, width)

Parameters :  



  • stat : Set the stat parameter to identify the mode.
  • fill : Represents color inside the bars.
  • color : Represents color of outlines of the bars.
  • width : Represents width of the bars.

Data in use:

Let us see what the default plot will look like without any modifications.

R




library(ggplot2)
  
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
                   runs=c(67,37,74,10))
head(ODI)
  
# Default axis labels in ggplot2 bar plot
perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+
  geom_bar(stat="identity")
perf

Output:

By default, R adds the vector names which are assigned in the Data Frame as the axis title. To manually add axis title use the following commands :

// To modify the x axis label



xlab(“X_axis_Labelname”) 

// To modify the y axis label 

ylab(“Y_axis_Labelname”) 

// Simultaneously modify both x and y axes title

labs(x=”X_axis_Labelname”,y=”Y_axis_Labelname”) 

Example:

R




library(ggplot2)
  
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
                   runs=c(67,37,74,10))
head(ODI)
  
# Default axis labels in ggplot2 bar plot
perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+
  geom_bar(stat="identity")
perf
  
# Manually adding axis labels 
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp

Output:

 

To perform any modifications in the axis labels we use the function element_text( ). The arguments of this function are :



  • Color
  • Size
  • Face
  • Family
  • lineheight
  • hjust and vjust

The argument hjust (Horizontal Adjust) or vjust (Vertical Adjust) is used to move the axis labels. They take numbers in range [0,1] where :

// Depicts left most corner of the axis

hjust = 0 

// Depicts middle of the axis

hjust = 0.5

// Depicts right most corner of the axis 

hjust = 1 

Let us first create a plot with axis labels towards the left.

Example:

R




library(ggplot2)
  
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
                   runs=c(67,37,74,10))
head(ODI)
  
# Default axis labels in ggplot2 bar plot
perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+
  geom_bar(stat="identity")
perf
  
# Manually adding axis labels 
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
  
# Moving axis label to left
ggp + theme(
  axis.title.x = element_text(hjust=0),
  axis.title.y = element_text(hjust=0)
  )

Output:

Now lets move them to the center.

Example:

R




library(ggplot2)
  
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
                   runs=c(67,37,74,10))
head(ODI)
  
# Default axis labels in ggplot2 bar plot
perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+
  geom_bar(stat="identity")
perf
  
# Manually adding axis labels 
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
  
# Moving axis label in middle
ggp + theme(
  axis.title.x = element_text(hjust=0.5),
  axis.title.y = element_text(hjust=0.5)
)

Output:

Similar can be done for moving them towards the right.

Example:

R




library(ggplot2)
  
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
                   runs=c(67,37,74,10))
head(ODI)
  
# Default axis labels in ggplot2 bar plot
perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+
  geom_bar(stat="identity")
perf
  
# Manually adding axis labels 
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
  
# Moving axis label to right
ggp + theme(
  axis.title.x = element_text(hjust=1),
  axis.title.y = element_text(hjust=1)
)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!