'Using ggplot for confidence intervals display

I've got to build a plot of mean values and 99% confidence intervals (based on t-distribution) of data set cuckoos from package DAAG.

Here is my solution

ggplot(data=cuckoos, aes(x=species, y=length, colour=species)) +
    stat_summary(geom='pointrange', fun.args = list(mult=1)) +
    theme_dark() + 
    theme(legend.position="none", 
          axis.text = element_text(size=10), 
          axis.text.x = element_text (angle = 45, hjust = 1)) + 
    scale_colour_brewer(palette = "Pastel1") + 
    coord_cartesian(ylim=c(21,24))

The confidence intervals of the resulting plot do not match the right answer (see the images attached). What the problem might be?

enter image description here enter image description here

r


Solution 1:[1]

The default function of the stat_summary function is mean_se, meaning that you're actually getting the mean and a standard deviation. If you want to calculate the 99% confidence interval, you need to use the mean_cl_normal function and specify that you want to get the 0.99 confidence interval, instead of the default 0.95.

ggplot(data= DAAG::cuckoos, aes(x=species, y=length, colour=species)) +
    stat_summary(geom='pointrange', fun.data = "mean_cl_normal", fun.args = list(conf.int = 0.99)) + 
    theme_dark() + 
    theme(legend.position="none", axis.text = element_text(size=10), axis.text.x = element_text (angle = 45, hjust = 1)) +
    scale_colour_brewer(palette = "Pastel1") +
    coord_cartesian(ylim=c(21,24))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergio Romero