0
votes

Following the R cookbook example I created a graph by only using one variable and have it's count be on the y axis:

ggplot(data_set, aes(x = species)) + geom_bar() 

However when I try to add labels on the bar chart with the

geom_text()

function to show the percentage of each bar I keep running into errors like:

Error: geom_text requires the following missing aesthetics: y

or

Error: Aesthetics must be either length 1 or the same as the data (31357)

I tried suggestions like: How to add percentage or count labels above percentage bar plot?

but they keep giving me these errors. I think it is a special case as I am only using one column and having geom_bar() automtically count it for me.

This feels very simple to do but nothing I do works, any help would be appreciated.

1
Pls provide a minimal reproducible example with a description of your ideal outputKeith McNulty

1 Answers

1
votes

Firstly, please provide reproducible examples for your questions. I was going through R Cookbook recently and remember this actually and it seems you are referring to the iris dataset as you mention species.

Secondly, I'm not sure what you're trying to achieve here in terms of adding percentages as bar plot for each species would be 100%...

Thirdly, the answer for adding counts is actually in the link you mention if you look closely!

Here's the solution for counts; you need to specify the label and statistic, otherwise R won't know.

library(tidyverse)

ggplot(iris, aes(x = Species)) + 
  geom_bar() +
  geom_text(aes(label = ..count..), stat = "count", vjust = 1.5, colour = "white")

enter image description here