0
msleep %>%
  group_by(vore) %>%
  na.omit() %>% 
  summarise(length_mean = mean(sqrt(bodywt)),
            brain_mean = mean(sqrt(brainwt))) %>%

  ggplot(aes(vore, length_mean, fill=vore)) +
  geom_bar(stat = 'identity', width = brain_mean) + 
  scale_fill_brewer(palette = 'Blues')+
  coord_flip() + 
  scale_x_discrete(limits = c("herbi", "carni", "omni", 'insecti')) +   
  ggtitle('avarage of the mean by sex') + 
  geom_text(aes(label = (round(length_mean, 3))), 
            vjust = 0.5, hjust = 1.1, color = "black", size = 3.5)

This is the error that appears when I write the above code:

'Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomBar, :object 'brain_mean' not found'

I tried to change many options, such as to insert the option

'inherit.aes = FALSE'

but it seems not to work.

What is my error?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Please add sample data using `dput`. You can find more details [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – NelsonGon Mar 02 '19 at 12:12
  • 2
    I disagree with the comments above. `msleep` is a sample dataset from the `ggplot2` package, so the problem **is** reproducible. And width is usable within `aes()` for `geom_bar` / `geom_col` even if it does trigger a warning. You just need to put it inside `aes()`. I.e. `geom_bar(stat='identity', aes(width = brain_mean))` or `geom_col(aes(width = brain_mean))`. – Z.Lin Mar 02 '19 at 13:44
  • That said, it *is* a weird way to visualise data. A mosaic plot uses bar width, but usually the height of its bars correspond to a 100% stacked chart. Varying both bar width and bar height is rare, & hence likely to confuse the audience. – Z.Lin Mar 02 '19 at 13:47
  • if I use geom_bar(stat='identity', aes(width = brain_mean)) it works and the graph is correct but a warining appers: Warning: Ignoring unknown aesthetics: width thank you so much for your help – Massimo Checchin Mar 02 '19 at 14:07

0 Answers0