Display All X-Axis Labels of Barplot in R
While working on bar plots, there might be a situation where all the labels in the X-axis might not be visible due to the length of the variable names. This article deals with resolving the problem in the R programming language.
Method 1: Using barplot()
In R language barplot() function is used to create a barplot. It takes the x and y-axis as required parameters and plots a barplot. To display all the labels, we need to rotate the axis, and we do it using the las parameter. To rotate the label perpendicular to the axis we set the value of las as 2, and for horizontal rotation, we set the value as 1. Secondly, to increase the font size of the labels we use cex.names to set the font size of the labels.
Syntax: barplot(data, xlab, ylab)
data is the data vector to be represented on y-axis
xlab is the label given to x-axis
ylab is the label given to y-axis
Method 2: Using ggplot
Another most popular library for drawing plots in R is ggplot2. We draw box plots using geom_bar() functions in ggplot2. To specify the changes are for the x-axis we use axis.text.x parameter in theme() function and use element_text() to specify the angle and font size.