Showcasing 2 error bars based on seperate functions

Viewed 29

My goal is to showcase two error bars as point ranges for the first and second standard deviation of my data. I've created two functions, "data_summary", and "data_summary_2", which I input into 'stat_summary', but I only get the second one drawn on my ggplot. I've marked where I'm having issues within my code. enter image description here

How can I create a plot that will showcase both of these error bars? I tried to summarize the data prior and merge it with the original datafile, but the error bars turned out wonky.

    df <- read.csv("data.csv")

# load ggplot2
library(ggplot2)
library(hrbrthemes)
library(tidyverse)
library(dplyr)
library(EnvStats)


#Creating an empty list to save plots crated. Lists in R are very versatile.
#They can pretty much store any type of data in them
quantile_plot = list()

#Creating an empty list to save plots crated. Lists in R are very versatile.
#They can pretty much store any type of data in them
quantile_final = list()

#getting all the congeners that will be looped over

congener_list = unique(df$Congener)

#order by ascending and descending order for graphic to match up
df$Congener = with(df, reorder(Congener, order_num))

#looping over unique congener names
for (i in congener_list) {

#Get mean & Standard Deviation by Group
  data_msd <- df %>%                           
    group_by(Ethnicity_category, Congener) %>%
    summarise_at(vars(y),
                 list(mean = mean,
                      sd = sd)) %>% 
    as.data.frame()
  data_msd
  
#merge these stats to main data frame
  df_merge <- merge(df, data_msd, by = c("Ethnicity_category", "Congener"))
  
#Make stat function for 1STDEV
  data_summary <- function(x) {
    m <- mean(x)
    ymin <- m-sd(x)
    ymax <- m+sd(x)
    return(c(y=m,ymin=ymin,ymax=ymax))
  }
  
  #Make stat function for 2STDEV
  data_summary_2 <- function(x) {
    m <- mean(x)
    ymin <- m-2*sd(x)
    ymax <- m+2*sd(x)
    return(c(y=m,ymin=ymin,ymax=ymax))
  }
  
df_merge %>% filter(Congener == i) %>%
ggplot(aes(x = Ethnicity_category, y = y, color = Ethnicity_category, group = Ethnicity_category)) +
            labs(title =paste0('2003-2004 NHANES Males 60+ ', i), x = 'Ethnicity', y = 'ng/g Lipid Weight') +
            scale_y_continuous(trans = 'log10') +
            scale_color_brewer(palette = "Dark2") +
            theme_light() +
            stat_summary(fun.data = data_summary, color = "blue") +
            stat_summary(fun.data = data_summary_2, color = "blue")   #where I have the issue
            stat_n_text() +
            geom_jitter(
                shape = 19, alpha = 0.5, size = 5) +
                theme(axis.text.y = element_text(size = 15),
                      axis.text.x = element_text(size = 15),
                      axis.title = element_text(size = 15),
                      plot.title = element_text(size = 17),
                      legend.text = element_text(size=15)) -> quantile_plot[[i]]

#save to plots to disk
  ggsave(quantile_plot[[i]], file=paste0('V3_log_2003_2004_trends_', i, '.png'),
         width = 44.45, height = 27.78, units = 'cm', dpi = 600)
}
0 Answers
Related