I have a dataframe that contains information on business bids. I want to create a histogram that shows the percentage of bids that were booked for each bin. The x axis should be price, and the y axis should be the percent of bids that booked within that bin.
my code filters the data for two years, then uses the ggplot 2 library to create the histogram. In this example, "booked" is a logical variable, and "price" is a continuous number. Heres what I have for code:
exp_data %>% filter(yr>=2019 & yr<=2020) %>%
ggplot(aes(price)) +
geom_histogram(aes(y=mean(booked)),binwidth=500)
When I run the code I get the following error:
Error: stat_bin() must not be used with a y aesthetic.
I've also tried this
exp_data %>% filter(yr>=2019 & yr<=2020) %>%
ggplot(aes(price)) +
geom_histogram(aes(y=((..booked==TRUE..)/(..count..))),binwidth=500)
That code produces this error: Error in FUN(X[[i]], ...) : object '..booked' not found
I'm guessing I'm simply ignorant of the right way to ask for the math functions I'm trying to get. Can someone help me learn how to do this?
here's a sample data frame:
'''
exp_data <- data.frame(price=abs(rnorm(100))*10000,
booked=sample(c(TRUE,FALSE),size=100,replace = TRUE))
with this code, I can plot the counts of true & false & stack/dodge them, but all I really want to know is the percent of true.
exp_data %>% ggplot(aes(price)) + geom_histogram(aes(fill=booked),position="dodge")