How to change the line thickness of whiskers using stat_boxplot(geom = "errorbar")

11,168

Solution 1

If I understand your question, I think this is what you're looking for:

ggplot(df, aes(x=cond, y=value)) + 
  stat_boxplot(geom = "errorbar", width=0.5, size=5) +
  geom_boxplot(lwd=0.2)

Here's the result with two different width settings:

enter image description here

Solution 2

Try

ggplot(df, aes(x=cond, y=value)) + stat_boxplot(geom = "errorbar", 
stat_params = list(width = 0.5), geom_params = list(size = 2))
+geom_boxplot(size = 2)
Share:
11,168
lince
Author by

lince

Updated on June 15, 2022

Comments

  • lince
    lince almost 2 years

    I would like to change the line thickness of the whiskers when using stat_boxplot(geom = "errorbar"):

    set.seed(42)
    df <- data.frame(cond = factor( rep(c("A","B"), each=500) ), 
      value = c(rnorm(500,mean=1,sd=0.2),rnorm(500, mean=1.5,sd=0.1)))
    ggplot(df, aes(x=cond, y=value)) + geom_boxplot(lwd=0.2)
    ggplot(df, aes(x=cond, y=value)) + 
          stat_boxplot(geom = "errorbar", 
           stat_params = list(width = 0.5,size = 5.0)) + 
          geom_boxplot(lwd=0.2)
    

    In the second plot lwd=0.2 changes the thickness of the lines in the box, but the whiskers remain the same.

    enter image description here enter image description here

    Update

    Thanks @eipi10,

    ggplot(df, aes(x=cond, y=value)) + stat_boxplot(geom = "errorbar",
        width = 0.5, size=0.2) + geom_boxplot(lwd=0.2)
    

    your solution changes the thickness of the lines of the whiskers but it makes the horizontal line at their end as wide as the box, instead of half (width = 0.5).

    But using

    ggplot(df, aes(x=cond, y=value)) + stat_boxplot(geom ="errorbar",
        stat_params = list(width = 0.5), size=0.2) + geom_boxplot(lwd=0.2)
    

    or

    ggplot(df, aes(x=cond, y=value)) + stat_boxplot(geom = "errorbar",
        stat_params = list(width = 0.5, size=0.2)) + geom_boxplot(lwd=0.2)
    

    then the whiskers width is half of the box as intended but their line thickness is the default one that is thicker than the lines of the box.

    In other words, I cannot simultaneously change the thickness of the lines and the width of the whiskers.

    Update two

    I am getting the same result with these two pieces of code (both without stat_params)

    ggplot(df, aes(x=cond, y=value)) + stat_boxplot(geom = "errorbar",
        width=0.5, size=5) + geom_boxplot(lwd=0.2)
    
    ggplot(df, aes(x=cond, y=value)) + stat_boxplot(geom = "errorbar",
        width=0.2, size=5) + geom_boxplot(lwd=0.2)
    

    enter image description here enter image description here

    Jose