Using hjust & vjust in ggplot2 Plots in R (Example Code)
In this article, I’ll show how to apply the hjust and vjust arguments in R.
Preparing the Example
data(iris) # Loading iris data set head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Loading iris data set head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2") |
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2")
my_plot <- ggplot(iris, # Create ggplot2 graph aes(x = Sepal.Width, y = Petal.Length)) + geom_point() my_plot # Draw ggplot2 graph |
my_plot <- ggplot(iris, # Create ggplot2 graph aes(x = Sepal.Width, y = Petal.Length)) + geom_point() my_plot # Draw ggplot2 graph
Example: Move X-Axis Position Using hjust & vjust
my_plot + # Apply hjust & vjust theme(axis.title.x = element_text(hjust = - 0.01, vjust = 50, colour = "red")) |
my_plot + # Apply hjust & vjust theme(axis.title.x = element_text(hjust = - 0.01, vjust = 50, colour = "red"))