简体   繁体   中英

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

When I resize whiskers in a boxplot using geom="errorbar, width=" the center line (from stat_boxplot) with whiskers moves, so I get two lines:

  • How can I avoid that when I resize? Especifically, I would like to make whisker width smaller thant boxplot width.

  • How do I hide the line from boxplot? (An answer for the first question would solves the problem, but I'm curious).

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)

I can get your desired outcome by adjusting the position of your stat_boxplot() . For me, it appears correct by adding the following argument: position = position_dodge(width = 0.75) . It was trial and error to get the correct value of 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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM