The goal of this tutorial is to rotate the x axis text.
# First we load the libraries
library(ggplot2)
library(dplyr)
# We are going to use the iris dataset
data("iris")
# We want to plot the average petal width per species
iris_avg_pw <- iris %>% group_by(Species) %>% summarise(Petal.Width = mean(Petal.Width)) %>% as.data.frame()
# Let's plot
ggplot(data = iris_avg_pw) + geom_col(aes( x = Species, y = Petal.Width))
# Now we can rotate the axis' texts
ggplot(data = iris_avg_pw) + geom_col(aes( x = Species, y = Petal.Width)) +
theme(axis.text.x=element_text(angle =- 90, vjust = 0.5))
# vjust = 0.5 puts the rotated text in the center of the tick
In this tutorial we have learnt how to rotate the x axis’ text. This could be useful for example when we want to plot dates and the text in the axis overlap.