Add names to values in histogram ggplot

Hey!
I´m trying to visualize some of my data wich I collected from a survey using a likert-scale. For that I´d like to use a Histogram or Barplot to see how often which option got chosen.

ggplot(data = umfrage_ba, aes(x=v_58)) +
  geom_histogram(binwidth = 0.5, color = "white", fill = "steelblue") +
  labs(x = "Relevanz", y = "Häufigkeit") +
  ggtitle("Histogramm für den akzeptieren Button") +
  theme(plot.title = element_text(hjust = 0.5))

another attempt:

hist(umfrage_ba$v_58, col="steelblue", labels = TRUE,ylim = c (0, 50), 
     breaks = c(0.5,1.5,2.5,3.5,4.5,5.5), xlim = c(0,6), main = "Boxplott des akzeptieren Buttons",
     xlab = "Relevanz", ylab = "Anzahl")

I´d like to swap the 1 - 5 with the actually names of the values. From not very important to very important. Is there an easy way to do so? Also I´d like to show the absolute numbers of each value, for example 1 got clicked 48 times.

If you have a column in your data frame that lists the individual responses, you can use geom_bar as in the following code.

library(ggplot2)
DF <- data.frame(Response = sample(1:5, 100, replace = TRUE))
head(DF)
#>   Response
#> 1        5
#> 2        1
#> 3        1
#> 4        4
#> 5        4
#> 6        1
DF$Response <- as.character(DF$Response)
ggplot(DF, aes(x = Response)) + 
  geom_bar(fill = "steelblue", color = "white") +
  scale_x_discrete(labels = c("Very Neg.", "Neg", "Neutral", 
                              "Pos", "Very Pos"))

Created on 2022-07-25 by the reprex package (v2.0.1)

To show the actual counts, I would summarize the data first and use geom_col.

library(dplyr)
library(ggplot2)
DF |> count(Response) |> 
  ggplot(aes(x = Response, y = n)) + 
  geom_col(fill = "steelblue", color = "white") +
  geom_text(aes(label = n), vjust = 1)

Thanks a lot for the fast reply, works really good for me!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.