ggplot::geom_boxplot() How to change the width of one box group in R

前端 未结 2 1862
野趣味
野趣味 2021-01-13 01:06

I want to adapt the width of the box in the category \"random\" to the same width of the other boxes in the plot. It is now a single group, whereas the other groups contain

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 01:26

    Starting in ggplot2 3.0.0 there is a preserve argument in position_dodge() that allows the width of a single element to be preserved. There is also a second dodging function, position_dodge2(), which changes how elements are spread over the plotting area with overlap.

    If you want to have all boxes the same width with the single box centered on its x tick, you can use preserve = "single" in position_dodge2().

    ggplot(TablePerCatchmentAndYear, aes(x = NoiseType, y = POA, fill = TempRes)) + 
         geom_boxplot(lwd = 0.05, position = position_dodge2(preserve = "single") ) + 
         ylim(-1.25, 1) + 
         theme(legend.position='bottom') + 
         scale_fill_discrete(name = '')
    

    If you want to have all boxes the same width and mirror the dodging for the single element group with the other groups you can add preserve = "single" to position_dodge().

    ggplot(TablePerCatchmentAndYear, aes(x = NoiseType, y = POA, fill = TempRes)) + 
         geom_boxplot(lwd = 0.05, position = position_dodge(preserve = "single") ) + 
         ylim(-1.25, 1) + 
         theme(legend.position='bottom') + 
         scale_fill_discrete(name = '')
    

提交回复
热议问题