Say I have some data that looks a bit like this
library(ggplot2)
library(dplyr)
employee <- employee <- c('John','Dave','Paul','Ringo','George','Tom','Jim','Harry','Jamie','Adrian')
quality <- c('good', 'bad')
x = runif(4000,0,100)
y = runif(4000,0,100)
employ.data <- data.frame(employee, quality, x, y)
And I'm working with a geom_bin2d plot that looks like this
ggplot(dat, aes(x, y)) +
geom_bin2d(binwidth = c(20, 20)) +
scale_fill_gradient2(low="darkred", high = "darkgreen")
plot
How can I change the colour of the bins to reflect the percentage of the x/y points that are 'bad' compared to the overall average in that area across the dataset? I.e, if the average of 'bad' points in the bottom left bin is x number, and the average for John in that area is y lower number, how can I make the bin colour darker to show that his count is lower?
I figured this could work to create the averages:
df2 <- employ.data
df2$xbin <- cut(df2$x, breaks = seq(0, 100, by = 20))
df2$ybin <- cut(df2$y, breaks = seq(0, 100, by = 20))
df2 <- df2 %>% group_by(xbin, ybin) %>% mutate(ave_pct = mean(quality == "bad"))
df2 <- df2 %>% group_by(employee, xbin, ybin) %>% mutate(person_pct = mean(quality == "bad"))
But then I have no idea how to plot that.
