R ggplot2 boxplot - varying box width by a function/vector values

南楼画角 提交于 2020-01-16 08:45:03

问题


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?


回答1:


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))


来源:https://stackoverflow.com/questions/48808447/ggplot-width-of-boxplot-from-summary-stats

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!