Question

I have a boxplot with a dot representing the mean:

Boxplot with mean

This is alright, but I'd like to show the confidence interval around the mean, for example in the following way:

Diamond plot

How can I overlay these two plots in R?

Was it helpful?

Solution

A function like the following is easy to write. Enter the x and y for the mean, the upper limit, lower limit, and width of the diamond. You can pass other parameters like lty for line type and col colour to segments as well.

diamondCI <- function(x, y, ul, ll, w = ul-ll, ...){
    hw <- w/2
    segments(x-hw, y, x+hw, y, ...)  # horizontal bar
    segments(x-hw, y, x, ul, ...) # left upper diag
    segments(x, ul, x+hw, y, ...) # right upper diag
    segments(x-hw, y, x, ll, ...) # left lower diag
    segments(x, ll, x+hw, y, ...) # right lwoer diag
    }

Try it out with...

plot(1,1)
diamondCI(1, 1, 1.2, 0.7, col = 'red', lwd = 3)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top