使用分组变量在 ggplot 箱线图中调整晶须(宽度)的大小

Resize whiskers (width) in a ggplot boxplot with a grouping variable

当我使用 geom="errorbar , width="中心线(来自 stat_boxplot)和胡须移动调整箱线图中的胡须大小时,我得到两条线:

library(car)
data(Salaries)    # 'data(Salaries, package=car)' doesn't work 
library(ggplot2)

p<- ggplot(Salaries, aes(x=rank, y=salary, fill=sex)) +
  stat_boxplot(geom= 'errorbar', width = 0.3) +
  geom_boxplot() + 
  labs(title="Salary by Rank and Sex", x="Rank", y="Salary") 
  
show(p)

我可以通过调整你stat_boxplot()的位置得到你想要的结果。对我来说,添加以下参数似乎是正确的:position = position_dodge(width = 0.75)。获得 0.75 的正确值是反复试验。

p <- ggplot(Salaries, aes(x=rank, y=salary, fill=sex)) +
  stat_boxplot(geom= 'errorbar' , width = 0.3, position = position_dodge(width = 0.75) ) +
  
  geom_boxplot() +
  
  labs(title="Salary by Rank and Sex", x="Rank", y="Salary") 

show(p)