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

前端 未结 1 1299
执笔经年
执笔经年 2020-12-22 09:49

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

相关标签:
1条回答
  • 2020-12-22 10:04

    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))
    
    0 讨论(0)
提交回复
热议问题