Marc Brinkmann
Marc Brinkmann

Reputation: 142

How to add labels for each bar (count on y-axis)?

I'm currently trying to recreate image shown below. I have troubles with adding labels to each bar. I've also already searched google and SO for solutions, but I wasn't able to adapt them to my problem. There's no need to achieve an exact 100% copy.

Current State: Solved

Current State Graphic

Target

Target Graphic

Code

I've written a function 'createBarplot' which takes dataframe (obviously a data frame) and x (string/char) as parameters. Don't ask why. In any case, this is the reason for the code looking like this.

ggplot(dataframe, aes(x = dataframe[, x])) +
    geom_bar(aes(y = (..count..)/sum(..count..)), fill="#0081DB", colour="#000F83", size=2) +
    scale_y_continuous(labels = scales::percent, breaks = seq(0, 0.6, by = 0.1)) +
    scale_x_discrete(labels = levels(dataframe[, x])) +
    labs(y = "Prozent", x = NULL) +
    ggtitle(label = attr(sampledata, "variable.labels")[do.call("getColumnIndexByName",args = list(dataframe, x))]) +
    geom_text(aes(y = ..count.. / sum(..count..),
                  label = paste0(round(..count.. / sum(..count..) * 100, 0),"%"),
                  hjust = ifelse( (..count.. / sum(..count..)) >= 0.025, 1.25, -0.25)),
              stat="count") +
    theme(panel.background = element_rect(fill = "white"),
          axis.line = element_line(colour = "black"),
          plot.title = element_text(hjust = 0.1)) +
    coord_flip()

Data

dataframe <- as.data.frame(x = c(5L, 2L, 4L, 5L, 5L, 5L, 4L, 4L, 2L, 4L, 4L, 4L, 5L, 
                                           3L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 5L, 5L, 3L, 4L, 4L, 3L, 4L, 4L, 
                                           3L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 3L, 5L, 5L, 4L, 4L, 5L, 5L, 
                                           5L, 4L, 4L, 2L, 5L, 4L, 4L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 
                                           4L, 4L, 4L, 4L, 4L, 3L, 2L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 
                                           4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 5L, 3L, 
                                           4L, 4L, 4L, 3L, 2L, 5L, 3L, 4L, 3L, 3L, 4L, 4L, 5L, 4L, 4L, 2L, 
                                           5L, 3L, 4L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 4L, 2L, 3L, 4L, 4L, 4L, 
                                           4L, 5L, 4L, 3L, 3L, 5L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 5L, 4L, 5L, 
                                           4L, 4L, 4L, 3L, 4L, 3L, 2L, 4L, 3L, 4L, 4L, 4L, 4L, 3L, 4L, 5L, 
                                           4L, 4L, 4L, 4L, 4L, 5L, 4L, 3L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 
                                           5L, 5L, 4L, 4L, 4L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 5L, 4L, 
                                           3L, 5L, 4L, 3L, 3L, 5L, 4L, 4L, 5L, 4L, 4L, 5L, 5L, 4L, 5L, 4L, 
                                           5L, 3L, 3L, 4L, 5L, 4L, 3L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                           5L, 4L, 4L, 3L, 4L, 3L, 4L, 5L, 4L, 4L, 2L, 5L, 4L, 5L, 2L, 3L, 
                                           3L, 4L, 4L, 4L, 5L, 5L, 3L, 4L, 4L, 5L, 4L, 4L, 4L), .Label = c("5 Sehr unzufrieden", 
                                                                                                           "4", "3", "2", "1 Sehr zufrieden"), class = "factor")

colnames(dataframe) <- "col"
x <- "col"

Upvotes: 2

Views: 1419

Answers (1)

lukeA
lukeA

Reputation: 54237

You could do

library(tidyverse)
dataframe %>% 
  ggplot(aes_string(x = x)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), stat = "count") + 
  coord_flip() + 
  geom_text(aes(y=..count../sum(..count..), label=paste0(..count../sum(..count..)*100,"%")), hjust=1, stat="count")

However, if you compare it with the pre-counted

dataframe %>% 
  count_(x) %>%
  mutate(n=n/sum(n)*100) %>%
  ggplot(aes(x=col, y=n)) + 
  geom_col() + 
  coord_flip() + 
  geom_text(aes(label = paste0(n, "%")), hjust=1)

I'd say the latter one is more readable (and for larger data sets probably faster, too). Both should give you the same result.

Upvotes: 1

Related Questions