hnguyen
hnguyen

Reputation: 814

Object not found in `geom_bar()`

I want to map prop to the y-axis on my bar chart but I keep getting the "object not found" error. How do I fix my code? Thank you!

dput(starwars_age)
structure(list(`Age Response` = c("> 60", "18-29", "30-44", "45-60"
), n = c(193L, 180L, 207L, 240L), prop = c(0.206196581196581, 
0.192307692307692, 0.221153846153846, 0.256410256410256)), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

starwars_age %>%
  mutate(`Age Response` = factor(`Age Response`, levels  = c("18-29","30-44","45-60","> 60"))) %>%
  ggplot(aes(x = `Age Response`))+
  geom_bar(aes(y = prop))

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

Upvotes: 0

Views: 1009

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

We can add stat = 'identity' in geom_bar :

library(dplyr)
library(ggplot2)

starwars_age %>%
  mutate(`Age Response` = factor(`Age Response`, 
                          levels  = c("18-29","30-44","45-60","> 60"))) %>%
  ggplot(aes(x = `Age Response`)) + 
  geom_bar(aes(y = prop), stat = 'identity')

enter image description here

Upvotes: 1

Related Questions