R How to Show All Axis Labels of Barchart (2 Examples)
In this R tutorial you’ll learn how to display all text labels of a barchart axis.
Preparing the Examples
data(iris) # Load iris iris_new <- iris[c(1, 51, 101), ] # Modify iris iris_new # Example data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 51 7.0 3.2 4.7 1.4 versicolor # 101 6.3 3.3 6.0 2.5 virginica |
data(iris) # Load iris iris_new <- iris[c(1, 51, 101), ] # Modify iris iris_new # Example data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 51 7.0 3.2 4.7 1.4 versicolor # 101 6.3 3.3 6.0 2.5 virginica
barplot(iris_new$Petal.Length ~ # Draw regular barplot in Base R iris_new$Species) |
barplot(iris_new$Petal.Length ~ # Draw regular barplot in Base R iris_new$Species)
Example 1: Reducing Size & Changing Angle to Display All Axis Labels of Base R Plot
barplot(iris_new$Petal.Length ~ # Barplot with smaller labels iris_new$Species, las = 2, cex.names = 0.5) |
barplot(iris_new$Petal.Length ~ # Barplot with smaller labels iris_new$Species, las = 2, cex.names = 0.5)
Example 2: Reducing Size & Changing Angle to Display All Axis Labels of ggplot2 Plot
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2") |
install.packages("ggplot2") # Install & load ggplot2 package library("ggplot2")
ggplot(iris_new, # Barplot with smaller labels aes(Species, Petal.Length)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, size = 5)) |
ggplot(iris_new, # Barplot with smaller labels aes(Species, Petal.Length)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, size = 5))