How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot? 2

How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot?

The hjust and vjust parameters in ggplot2 allow for precise adjustments to the horizontal and vertical positioning of elements within a plot. These parameters can be specified within the ggplot2 function or within individual layers, providing flexibility in the placement of titles, labels, and other graphical elements. By adjusting the hjust and vjust parameters, users can ensure that their plots are visually appealing and effectively convey their data.

Use hjust & vjust to Move Elements in ggplot2


You can use the hjust and vjust arguments to move elements horizontally and vertically, respectively, in ggplot2.

The following examples show how to use hjust and vjust in different scenarios.

Example 1: Move Title Position in ggplot2

The following code shows how to create a scatter plot in ggplot2 with a title in the default position (left-aligned):

library(ggplot2)

#create scatter plot with title in default position
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
  geom_point() +
  ggtitle("Plot Title") 

And the following code shows how to center-align the title by using hjust=0.5:

library(ggplot2)

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

Note: You can also use hjust=1 to right-align the title.

Example 2: Move Axis Label Position in ggplot2

The following code shows how to create a bar chart in ggplot2 in which the x-axis labels are rotated 90 degrees to make them easier to read:

library(ggplot2)

#create data frame
df = data.frame(team=c('The Amazing Amazon Anteaters',
                       'The Rowdy Racing Raccoons',
                       'The Crazy Camping Cobras'),
                points=c(14, 22, 11))

#create bar plot to visualize points scored by each team
ggplot(data=df, aes(x=team, y=points)) +
  geom_bar(stat='identity') +
  theme(axis.text.x = element_text(angle=90)) 

We can use the hjust and vjust arguments to adjust the x-axis labels so that they line up more closely with the tick marks on the x-axis:

library(ggplot2)

#create data frame
df = data.frame(team=c('The Amazing Amazon Anteaters',
                       'The Rowdy Racing Raccoons',
                       'The Crazy Camping Cobras'),
                points=c(14, 22, 11))

#create bar plot to visualize points scored by each team
ggplot(data=df, aes(x=team, y=points)) +
  geom_bar(stat='identity') +
  theme(axis.text.x = element_text(angle=90, vjust=.5, hjust=1)

 

Example 3: Move Text Position in ggplot2

The following code shows how to create a scatter plot in ggplot2 with annotated text for each point in the plot:

library(ggplot2)

#create data frame
df <- data.frame(player=c('Brad', 'Ty', 'Spencer', 'Luke', 'Max'),
                 points=c(17, 5, 12, 20, 22),
                 assists=c(4, 3, 7, 7, 5))

#create scatter plot with annotated labels
ggplot(df) +
  geom_point(aes(x=points, y=assists)) + 
  geom_text(aes(x=points, y=assists, label=player)) 

We can use the vjust argument to move the text elements up vertically so that they’re easier to read:

library(ggplot2)

#create data frame
df <- data.frame(player=c('Brad', 'Ty', 'Spencer', 'Luke', 'Max'),
                 points=c(17, 5, 12, 20, 22),
                 assists=c(4, 3, 7, 7, 5))

#create scatter plot with annotated labels
ggplot(df) +
  geom_point(aes(x=points, y=assists)) + 
  geom_text(aes(x=points, y=assists, label=player), vjust=-.6)

We could also use a positive value for vjust to move the text elements down vertically:

library(ggplot2)

#create data frame
df <- data.frame(player=c('Brad', 'Ty', 'Spencer', 'Luke', 'Max'),
                 points=c(17, 5, 12, 20, 22),
                 assists=c(4, 3, 7, 7, 5))

#create scatter plot with annotated labels
ggplot(df) +
  geom_point(aes(x=points, y=assists)) + 
  geom_text(aes(x=points, y=assists, label=player), vjust=1.2)

The annotated text is now located below each point in the plot.

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

Cite this article

stats writer (2024). How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-hjust-and-vjust-parameters-in-ggplot2-to-adjust-the-positioning-of-elements-within-my-plot/

stats writer. "How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-hjust-and-vjust-parameters-in-ggplot2-to-adjust-the-positioning-of-elements-within-my-plot/.

stats writer. "How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-hjust-and-vjust-parameters-in-ggplot2-to-adjust-the-positioning-of-elements-within-my-plot/.

stats writer (2024) 'How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-hjust-and-vjust-parameters-in-ggplot2-to-adjust-the-positioning-of-elements-within-my-plot/.

[1] stats writer, "How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use the hjust and vjust parameters in ggplot2 to adjust the positioning of elements within my plot?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top