Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box plot showing mean as a line

Tags:

r

mean

boxplot

Is it possible to create a boxplot that shows both mean and median as a line with the standard boxplot function of R ? My current solution displays the mean as a cross:

set.seed(1234)
values <- runif(10,0,1)
boxplot(values)
points(mean(values),col="red",pch=4,lwd = 4)

Image of boxplot

like image 258
scs Avatar asked Nov 13 '15 09:11

scs


People also ask

Why does my Boxplot look like a line?

Another reason you get flat lines instead of boxes is that you summarized the numeric values in your table (eg calculating mean, median, etc.) and now boxplot() sees only a single value.

What does the line on a box plot mean?

In a box plot: The box represents the middle 50% or so of the numeric values. A horizontal line within the rectangle represents the median of all values (specifically, the value that is exactly in the middle of all values).

Is the line in a Boxplot the mean or median?

In a box plot, we draw a box from the first quartile to the third quartile. A vertical line goes through the box at the median. The whiskers go from each quartile to the minimum or maximum.

What does the line in the middle of a Boxplot mean?

Median. The median (middle quartile) marks the mid-point of the data and is shown by the line that divides the box into two parts. Half the scores are greater than or equal to this value and half are less.


2 Answers

The default plotting of boxplot makes the width of the box go from 0.8 to 1.2 in the x-axis.

You can hence draw a line for the mean with the below code:

lines(c(0.8, 1.2), rep(mean(values), 2), col="red", lwd = 2)

enter image description here

like image 67
Cath Avatar answered Oct 05 '22 03:10

Cath


For the sake of completeness, you could also overplot:

set.seed(753)
df <- data.frame(y=rt(100, 4), x=gl(5, 20))
bx.p <- boxplot(y~x, df)
bx.p$stats[3, ] <- unclass(with(df, by(y, x, FUN = mean)))
bxp(bx.p, add=T, boxfill="transparent", medcol="red", axes=F, outpch = NA, outlty="blank", boxlty="blank", whisklty="blank", staplelty="blank")

Explanation via @scs:

bxp$stats returns a matrix that contains the lower whisker, the lower hinge, the median, the upper hinge and the extreme of the upper whisker for each boxplot. The solution above overwrites the median specified in bx.p$stats[3, ] with the mean value. The bxp function is a function to plot boxplot objects.

Result:

enter image description here

like image 43
lukeA Avatar answered Oct 05 '22 03:10

lukeA