Reputation: 395
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?
Upvotes: 0
Views: 1579
Reputation: 11762
There is the possibility to weight each box plot via the weight
aesthetics.
You need to have the quantreg
package installed to use this. I guess you can just provide your function in there, I used for demonstration an exponent of the drivesCount
column. So you need to adapt this a little.
install.packages("quantreg")
ggplot(Data, aes(x=roadType, y=happyPercentage)) +
geom_boxplot(varwidth = TRUE, alpha=0.2, aes(weight=drivesCount^10)) +
theme(legend.position="none") +
labs(x = "Road Type", y = "Happy People Percent") +
theme(plot.title = element_text(hjust = 0.5))
Upvotes: 1