Add Count Labels on Top of ggplot2 Barchart in R (Example)

 

In this tutorial you’ll learn how to add the frequency count on the top of each bar of a ggplot2 barchart in R.

Table of contents:

If you want to learn more about these topics, keep reading…

 

Example Data, Packages & Basic Plot

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

set.seed(983274)                                          # Create random example data
data <- data.frame(x = sample(LETTERS[1:5], 100, replace = TRUE))
head(data)                                                # Print first lines of data
# x
# 1 D
# 2 C
# 3 B
# 4 B
# 5 C
# 6 C

Have a look at the previously shown output of the RStudio console. It shows that our example data has only one column containing alphabetic letters.

For the following R syntax, we also have to install and load the ggplot2 package:

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

Finally, let’s move on to the example code…

 

Example: Drawing Barplot with Values on Top

In this Example, I’ll explain how to plot frequency labels on top of each bar of a ggplot2 barplot. First, we need to compute the frequency count within each group of our data:

data_srz <- as.data.frame(table(data$x))                  # Summarize data
data_srz                                                  # Print summarized data
#   Var1 Freq
# 1    A   21
# 2    B   15
# 3    C   23
# 4    D   18
# 5    E   23

In the output of the RStudio console above, you can see how often each group is contained in our example data. Now, we can plot our data in a barchart with counting labels on top as shown below:

ggplot(data_srz, aes(x = Var1, y = Freq, fill = Var1)) +  # Plot with values on top
  geom_bar(stat = "identity") +
  geom_text(aes(label = Freq), vjust = 0)

 

r graph figure 1 add count labels on top ggplot2 barchart r

 

As visualized in Figure 1, the previously shown R programming syntax created a barplot with counts on top of each bar with the ggplot2 package.

 

Video, Further Resources & Summary

Do you want to learn more about drawing labels on top of bars? Then I can recommend watching the following video of my YouTube channel. In the video, I illustrate the R codes of this tutorial.

 

 

Furthermore, I can recommend having a look at some of the other tutorials of this website. I have published several tutorials already:

 

To summarize: At this point you should know how to print the count labels of each bar on the top of the barchart in the R programming language. Let me know in the comments section below, in case you have any additional questions.

 

8 Comments. Leave new

  • How would you handle this if you had multiple entries for A, B, C etc? I am getting stacked numbers on the bars because there are multiple rows with data (the rows denote users, the X is a factor, and the Y is a total per user).

    Reply
  • Sadaf Akhtar
    June 17, 2024 3:10 pm

    Hi
    I interested in adding label at the end of the multiples line by their respective groups.
    x=2000:2024
    y= 1:25
    religion= c(“Christians”, “Muslims”, “Hindus”, “Jews”, “Others”)
    However, I used this command

    geom_label_repel(data=death%>%filter(religion==”Christians”), aes(x=x, y=y, label=religion), color=religion), size=2.5)+

    Unfortunately. the plot is showing labels for all the points. But I am interested in only one label at the end of the line. Can you please help?

    Reply
  • Hiya!!

    I am following this step first.

    data <- data.frame(x = 1:10, y = c(3, 1, NA, 5, 5, NA, NA, 4, 3, 6))

    data_comp %filter(religion==”Christians”), aes(x=x, y=y, label=religion), color=religion), size=2.5)+

    It is not working in tis case.

    Please help

    Reply
  • data <- data.frame(x = 1:10, y = c(3, 1, NA, 5, 5, NA, NA, 4, 3, 6), religion=c("Christians", "Hindus", "Muslims", ""Christians", "Christians", "Christians", "Jews:, "Hindus", "Jews", "Hindus")

    data_comp <- data[!is.na(data$y), ]

    ggplot() +
    geom_line(data = data_comp, aes(x, y), linetype = "dashed") +
    geom_line(data = data, aes(x, y))

    data_label<-data
    data_label$religion<-NA
    data_label$label[which(data_label$s==max(data_label$s))]% %filter(religion==”Christians”), aes(x=x, y=y, label=religion), color=religion), size=2.5)+

    Is this correct?

    Reply
    • Hey,

      Please try this code:

      data <- data.frame(x = 1:10,
                         y = c(3, 1, NA, 5, 5, NA, NA, 4, 3, 6),
                         religion = c("Christians", "Hindus", "Muslims", "Christians", "Christians", "Christians", "Jews", "Hindus", "Jews", "Hindus"))
       
      data_label <- data[!is.na(data$y), ]
       
      max_x_by_religion <- aggregate(x ~ religion, data_label, max)
       
      for (i in 1:nrow(max_x_by_religion)) {
        religion <- max_x_by_religion$religion[i]
        max_x <- max_x_by_religion$x[i]
        data_label$label[data_label$religion == religion & data_label$x == max_x] <- religion
      }
       
      data_label
       
      library("ggplot2")
      library("ggrepel")
       
      ggplot(data_label, aes(x, y, col = religion)) +    # Draw ggplot2 plot with labels
        geom_line() +
        geom_label_repel(aes(label = label),
                         nudge_x = 1,
                         na.rm = TRUE) +
        theme(legend.position = "none")

      Regards,
      Joachim

      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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top