0

I'm trying to make a bar graph and add to it the percentages of each category. However when I try to run the code I get the following error message:

Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomLabel,  : 
  object 'share' not found

Here is my code:

base3 %>%
  group_by(country.x) %>% 
  summarise(total=sum(cost)) %>%
  mutate(countrygroup=sapply(country.x,cathegorize), countries=sapply(country.x,reverse_cathegorize), share=total/sum(total)) %>%
  ggplot() +
  geom_bar(aes(x=reorder(countries,total),y=total,fill=countrygroup),colour = 'black', alpha = 0.5,stat="identity")+
  geom_label(aes(x=reorder(countries,total),y=share),label = paste0(round((share), 3)*100, '%'))
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jun 17 '21 at 17:06

1 Answers1

0

A couple potential reasons:

  1. Your variable share isn't being created properly in mutate(); when you just run lines 1-4 (so, before ggplot()), what does your dataset look like?
  2. When you call round((share), 3), ggplot doesn't know that you're talking about a variable, since it's outside of aes(); try round((.$share), 3)
ila
  • 709
  • 4
  • 15