Average graph of a diamond on a box in R

I have a boxplot with a dot representing the mean:

Boxplot with mean

This is fine, but I would like to show the confidence interval around the mean, for example, like this:

Diamond plot

How can I overlay these two graphs in R?


source to share

1 answer

It is easy to write a function like the following. Enter x and y for mean, upper limit, lower limit, and diamond width. You can pass other parameters like lty for linetype and col color to segments.

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 with ...

diamondCI(1, 1, 1.2, 0.7, col = 'red', lwd = 3)




All Articles