Move Axis Labels in ggplot in R
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( ).
geom_bar(stat, fill, color, width)
- 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.
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
// To modify the y axis label
// Simultaneously modify both x and y axes title
To perform any modifications in the axis labels we use the function element_text( ). The arguments of this function are :
- 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.
Now lets move them to the center.
Similar can be done for moving them towards the right.