Display All X-Axis Labels of Barplot in R (2 Examples)

 

In this tutorial, I’ll show how to show every x-axis label of a barplot in R programming.

The article consists of these topics:

Let’s dive into it…

 

Example Data & Default Graphic

We’ll use the data below as basement for this R programming tutorial:

data <- data.frame(value = 11:30,    # Create example data
                   group = paste0("Group_", LETTERS[1:20]))
head(data)                           # Head of example data

 

table 1 data frame display all x axis labels barplot r

 

Have a look at the table that got returned by the previous syntax. It shows that our example data has 20 rows and two columns. Each row represents a different bar of our barplot.

As next step, we can draw our data with default specifications:

barplot(data$value ~ data$group)     # Default barchart in Base R

 

r graph figure 1 display all x axis labels barplot r

 

As shown in Figure 1, we have managed to create a bargraph by executing the previous code. However, since our graphic contains too many bars, not all axis labels are shown.

Let’s change that!

 

Example 1: Show All Barchart Axis Labels of Base R Plot

Example 1 explains how to display all barchart labels in a Base R plot.

There are basically two major tricks, when we want to show all axis labels:

Let’s do both in R:

barplot(data$value ~ data$group,     # Modify x-axis labels
        las = 2,
        cex.names = 0.7)

 

r graph figure 2 display all x axis labels barplot r

 

In Figure 2 you can see that we have created a barplot with 90-degree angle and a smaller font size of the axis labels. All text labels are shown.

 

Example 2: Show All Barchart Axis Labels of ggplot2 Plot

In this example, I’ll show how to display all axis text labels of a ggplot2 graph.

In order to use the functions of the ggplot2 package, we first have to install and load ggplot2:

install.packages("ggplot2")          # Install & load ggplot2 package
library("ggplot2")

Next, we can use the theme function and the axis.text.x argument to change the angle and decrease the font size of the axis labels:

ggplot(data, aes(group, value)) +    # ggplot2 plot with modified x-axis labels
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, size = 5))

 

r graph figure 3 display all x axis labels barplot r

 

As shown in Figure 3, the previous syntax has drawn a ggplot2 barplot in which all axis labels are displayed.

 

Video & Further Resources

Do you need more explanations on the R programming code of this tutorial? Then you may want to have a look at the following video of my YouTube channel. I illustrate the R code of this post in the video.

 

 

Furthermore, you may want to have a look at the other tutorials on my homepage. A selection of articles about graphics in R is shown below.

 

To summarize: At this point you should know how to display all text labels of a barchart axis in the R programming language. Please note that we could use the same kind of syntax to show all labels of other types of graphics such as boxplots or heatmaps.

Don’t hesitate to let me know in the comments, in case you have additional questions and/or comments. Besides that, please subscribe to my email newsletter to get updates on new tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

  • When I do the ggplot geom_bar, only a few X-axis labels show

    Reply
  • I am plotting the hourly time series data using ggplot2 and my time series data start from 2011 June from 08:00 to 16:00, this data goes up to 2023 June 08:00 to 16:00. The plot I get starts from 2015 to 2022 and is not showing all the x-axis. Can you please assist

    Reply
    • Hello Moses,

      If your ggplot2 plot is not displaying the entire range of your data, there are a few potential reasons and fixes you can try:

      • Expanding x-axis range using the scale_x_datetime() function.
      • Modifying date breaks using the data_breaks argument.
      • Rotating x-axis labels using element_text.
      • Converting x axis data to Posixct if it has not been converted yet.

      Here’s a a sample data and the visualisation code using all methods mentioned above.

      library(tibble)
      library(dplyr)
      library(lubridate)
      library(ggplot2)
       
      # Generating the hourly time series data (shorter span)
      set.seed(123)  # Setting seed for reproducibility
      data <- tibble(
        date_time = seq(from = as.POSIXct("2021-06-01 08:00:00"), 
                        to = as.POSIXct("2021-06-30 16:00:00"), 
                        by = "hour")
      ) %>%
        filter(hour(date_time) >= 8 & hour(date_time) <= 16) %>%
        mutate(value = rnorm(n(), mean = 100, sd = 10))  # Randomly generated values
       
      # Printing the first few rows
      head(data)
       
      # Visiualising time series
      ggplot(data, aes(x = date_time, y = value)) +
        geom_line() +
        scale_x_datetime(date_breaks = "1 day", date_labels = "%Y-%m-%d") +
        theme(axis.text.x = element_text(angle = 45, hjust = 1))

      Best,
      Cansu

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top