Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify whiskers of a boxplot in ggplot2?

Tags:

r

ggplot2

boxplot

I'll start with an MWE:

library(ggplot2)

p <- ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(am)))
p + geom_boxplot()

enter image description here

I'd like to modify the colour of the whiskers, e.g., set it to red. I don't think it's possible to do this directly both geom_boxplot, so this is my workaround:

library(Hmisc)

stat_sum_df <- function(fun, geom = "crossbar", ...) {
  stat_summary(fun.data = fun, geom = geom, width = 0.4, ...)
}

p + stat_boxplot(geom = 'linerange', colour = "red", position = "dodge) +
    stat_sum_df("median_hilow", conf.int = 0.5, position = "dodge") 

enter image description here

The line ranges are stacked on top of each other. So next try:

p + stat_boxplot(geom = 'linerange', colour = "red", position = position_dodge(width = .5)) +
   stat_sum_df("median_hilow",conf.int=0.5, position = position_dodge(width = .5))

enter image description here

Looks nicer, but now there is a fixed space between the boxes (compare cyl = 8 on first and third plot). As I'm going to use this code for different number of levels of am (of course in my real data, it's not am), I don't know in advance how wide the boxes themselves will be, so I can't set a fixed width for the linerange without specifying a fixed width for the boxes.

Is there a way either to selectively modify whiskers of a boxplot or to adjust space between linerange elements according to space between the boxes?

like image 765
AnjaM Avatar asked Feb 27 '14 12:02

AnjaM


People also ask

What is ggplot2 boxplot in R?

Introduction ggplot2.boxplot is a function, to plot easily a box plot (also known as a box and whisker plot) with R statistical software using ggplot2 package. It can also be used to customize quickly the plot parameters including main title, axis labels, legend, background and colors. ggplot2.boxplot function is from easyGgplot2 R package.

How to change the width of whisker line in R boxplot?

In R, by default the whisker lines are as wide as the box of the boxplot but it would be great if we reduce that width or increase it because it will get attention of the viewer in that way. This can be done by using the width argument inside the stat_boxplot function of ggplot2 package. Check out the below example to understand how it works.

What is a box-whisker plot (boxplot)?

The box-whisker plot (or a boxplot) is a quick and easy way to visualize complex data where you have multiple samples. A box plot is a good way to get an overall picture of the data set in a compact manner. To get started, you need a set of data to work with. Let’s consider the built-in ToothGrowth data set as an example data set.

How to add jitter over box plot in ggplot?

# Add jitter over box plot ggplot (ToothGrowth, aes (x=factor (dose), y=len, fill=factor (dose))) + geom_boxplot (outlier.shape=NA) + geom_jitter (position=position_jitter (0.2))


1 Answers

How about plotting two boxplots on top of each other. One with red lines and a second one on top without any wiskers at all.

p + geom_boxplot(color="red") + geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) 
like image 114
shadow Avatar answered Sep 28 '22 09:09

shadow