Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

boxplot

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

like image 921
lince Avatar asked Sep 13 '15 13:09

lince


2 Answers

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

like image 156
eipi10 Avatar answered Nov 15 '22 01:11

eipi10


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)
like image 33
apac14 Avatar answered Nov 14 '22 23:11

apac14