I have a data frame with several groups values, and I would like to have a boxplot per category (drawn together). I want to have each boxplot with a different width, based not on the rows count per category, but on a column sum.
For example, with the following data.frame:
Data <- data.frame(roadType = sample(c("Ramp", "Primary Street", "Highway"),100,replace=TRUE), drivesCount = sample(1:100,100,replace=TRUE), happyPercentage=sample(c(0,0.25,0.5,0.75,1),100,replace=TRUE))
I know there's a way to have varying width based on rows count, like this:
ggplot(Data, aes(x=roadType, y=happyPercentage)) + geom_boxplot(varwidth = TRUE, alpha=0.2) + theme(legend.position="none") + labs(x = "Road Type", y = "Happy People Percent") + theme(plot.title = element_text(hjust = 0.5))
But I want to have a plot with a boxplot for happyPercentage per roadType, with width based on the drivesCount proportion of the specific roadType out of the total drivesCount.
Is that possible? How can I do it?