1

I am trying to put label in my plot I am getting error:

    ggplot(aes(x = factor(Pclass), fill = factor(Survived)), data = train) +
    geom_bar(color = "black", width = 0.30) +
    xlab("People Class") +
    ylab("Count")+
    geom_text(aes(label = factor(Survived)))

  Error: geom_text requires the following missing aesthetics: y

How can I add label without using y aesthetics or please correct if I am doing wrong. I am using Titanic train.csv data set Titanic: Machine Learning from Disaster

Shubham Rajput
  • 192
  • 3
  • 12

2 Answers2

2

Maybe try

ggplot(mtcars, aes(factor(cyl), fill = factor(gear))) +
geom_bar() + 
stat_count(aes(label = ..count..), geom = "text")
zx8754
  • 52,746
  • 12
  • 114
  • 209
0

The selected answer is correct, but I think this is more consistent with the formatting you are using:

ggplot(aes(x = factor(Pclass), fill = factor(Survived)), data = train) +
  geom_bar(color = "black", width = 0.30) +
  xlab("People Class") +
  ylab("Count") +
  geom_text(aes(label=..count..), stat='count')
Nicko
  • 350
  • 2
  • 9