11.1 C
London
Sunday, July 7, 2024
HomeTidyverse in Rggplot2 in RHow to Use hjust & vjust to Move Elements in ggplot2

How to Use hjust & vjust to Move Elements in ggplot2

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...

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 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 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 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.

Additional Resources

The following tutorials explain how to perform other common tasks 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