Creating Bars of mean with Standard Error, Confidence Interval; and Boxplot

Hello,

I am trying to create a bar plot of mean alone with its standard error and confidence interval. In the attachment, I have attached the image of the graphs that I want to generate. Here the 1st graph of the image shows a bar of the mean alone with 2 standard errors and the 2nd graph shows a bar of the mean with 95% confidence interval. These were generated in SPSS.

In addition to this, I would like to generate a boxplot (similar to the last graph). I have given a link of my dataset also.

Can anybody please help me with the R codes to do the above stuffs?

Many thanks.

library(openxlsx)
library(ggplot2)
df <- read.xlsx("c:/users/fjcc/Documents/R/Play/Data.xlsx")
Mean <- mean(df$age)
SE_2 <- 2 * sd(df$age)/sqrt(nrow(df))


T_test <- t.test(df$age)
T_test
#> 
#>  One Sample t-test
#> 
#> data:  df$age
#> t = 369.33, df = 5533, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#>  23.31577 23.56461
#> sample estimates:
#> mean of x 
#>  23.44019

Conf_int <- T_test$conf.int

Plot_df <- data.frame(X = "age",
                      Avg = Mean, 
                      SE_high = Mean + SE_2, 
                      SE_low = Mean - SE_2, 
                      Conf_int_high = Conf_int[2], 
                      Conf_int_low = Conf_int[1])
#standard error
ggplot(Plot_df, aes(x = X, y = Avg, ymin = SE_low, ymax = SE_high)) +
  geom_point() + geom_errorbar(width = 0.1) + labs(x = "", y = "mean and 2* SE") +
  ylim(23.2, 23.6)

  
#Confidence Interval
ggplot(Plot_df, aes(x = X, y = Avg, ymin = Conf_int_low, ymax = Conf_int_high)) +
  geom_point() + geom_errorbar(width = 0.1) + 
  labs(x = "", y = "mean and Conf Interval") +
  ylim(23.2, 23.6)


#boxplot
ggplot(df, aes(x = "age", age)) + geom_boxplot(fill = "royalblue") + 
  labs(x = "", y = "")

Created on 2019-06-03 by the reprex package (v0.2.1)

1 Like

@FJCC

Many Many thanks for the help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.