0

I have made a ggplot bar chart showing counts on the y-axis and categorical values on the x-axis.

p <- ggplot(data=vanco, aes(x=factor(verdi_mengde)))+geom_bar()

[![enter image description here][1]][1]

I want to display the counts on the bars, and have been attempting to add the counts with geom_text:

p+geom_text(aes(label=..count..))

However, to my frustration is get an error message:

Error in FUN(X[[i]], ...) : object 'count' not found

What am I doing wrong???

1 Answers1

2

You were in the correct way, but you're missing some parameters yet. consider reading Chapter 3.9 of R Graphics Cookbook where this topic is covered.

library(ggplot2)

ggplot(mtcars, aes(x = factor(cyl))) +
  geom_bar() +
  geom_text(
    aes(label = ..count..),
    stat = "count",
    color = "white",
    vjust = 1
  )

Created on 2022-09-23 with reprex v2.0.2

Johan Rosa
  • 2,797
  • 10
  • 18