---
title: "R Notebook"
output: html_notebook
editor_options: 
  chunk_output_type: console
---
```{r}
install.packages("pacman")

pacman::p_load("stringr","gplots","ggplot2", "tibble", "dplyr", "tidyr", "readr", "RColorBrewer", "dendextend", "openxlsx","writexl", "tibble", "gridExtra", "gridtext",  "ggpubr","pracma", "ggh4x","eulerr", "marray", "ggplotify", "corrplot", "forcats", "biomaRt", "ggpattern", "emmeans", "ggsignif", "multcomp",  "pwr", "gtable", "extrafont", "nlme", "rstatix","ez", "car", "broom", "ggtext", "drc", "rlang", "grid")
```

#read-in of data
```{r}
ROTresults <- openxlsx::read.xlsx("ROT R data.xlsx")
ROTresults$experiment <- as.factor(ROTresults$experiment)

#create legends
colors <- c("lightgrey", "white")
labels <- c("ROT", "Clean")
legendsex <- grid.legend(labels = labels, pch = 22, gp = gpar(fill = colors, col="black", fontfamily = "sans", cex = 2), hgap = unit(15, "mm"), vgap = unit(12, "mm"))

colors2 <- c("white")
labels2 <- c("Clean")
legendsex2 <- grid.legend(labels = labels2, pch = 22, gp = gpar(fill = colors2, col="black", fontfamily = "sans", cex = 2), hgap = unit(15, "mm"), vgap = unit(12, "mm"))
```

#Experiment 1
```{r}
ROTresultsfilterCIL <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "one") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIL)) | 
         !(any(TIME == "PRE") & any(is.na(CIL))))

#make each set of data plotted in the right spot
ROTresultsfilterCIL$TIME <- factor(ROTresultsfilterCIL$TIME, levels = c("PRE", "6H"))

#get summary statistics for bars on graph
summary_statsCIL <- ROTresultsfilterCIL %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIL),
            sd_value = sd(CIL, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIL, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIL, ROT == "yes")

merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIL.clean, merged_data$CIL.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIL <- max(ROTresultsfilterCIL$CIL) * 1.6

y_positionROTcomparisonCIL <- max(ROTresultsfilterCIL$CIL) * 1.2

#plot of CIL
exp1CIL <- ggplot(data=ROTresultsfilterCIL, aes(x=ROT, y=CIL)) + geom_bar(data = summary_statsCIL,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIL,
            aes(x = ROT, y = CIL, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("A. Complex I leak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIL, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "one") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIP)) | 
         !(any(TIME == "PRE") & any(is.na(CIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIP$TIME <- factor(ROTresultsfilterCIP$TIME, levels = c("PRE", "6H"))

#get summary statistiCIP for bars on graph
summary_statsCIP <- ROTresultsfilterCIP %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIP),
            sd_value = sd(CIP, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIP, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIP, ROT == "yes")

merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIP.clean, merged_data$CIP.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIP <- max(ROTresultsfilterCIP$CIP) * 1.6

y_positionROTcomparisonCIP <- max(ROTresultsfilterCIP$CIP) * 1.2

#plot of CIP
exp1CIP <- ggplot(data=ROTresultsfilterCIP, aes(x=ROT, y=CIP)) + geom_bar(data = summary_statsCIP,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIP,
            aes(x = ROT, y = CIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("B. Complex I peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIP, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "one") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIIIP)) | 
         !(any(TIME == "PRE") & any(is.na(CIIIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIP$TIME <- factor(ROTresultsfilterCIIIP$TIME, levels = c("PRE", "6H"))

#get summary statistiCIIIP for bars on graph
summary_statsCIIIP <- ROTresultsfilterCIIIP %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIIIP),
            sd_value = sd(CIIIP, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIIIP, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIIIP, ROT == "yes")

merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIIIP.clean, merged_data$CIIIP.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.6

y_positionROTcomparisonCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.2

#plot of CIIIP
exp1CIIIP <- ggplot(data=ROTresultsfilterCIIIP, aes(x=ROT, y=CIIIP)) + geom_bar(data = summary_statsCIIIP,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIP,
            aes(x = ROT, y = CIIIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("C. Complex I+II peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIIIP, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIC <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "one") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIIIC)) | 
         !(any(TIME == "PRE") & any(is.na(CIIIC))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIC$TIME <- factor(ROTresultsfilterCIIIC$TIME, levels = c("PRE", "6H"))

#get summary statistiCIIIC for bars on graph
summary_statsCIIIC <- ROTresultsfilterCIIIC %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIIIC),
            sd_value = sd(CIIIC, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIIIC, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIIIC, ROT == "yes")

merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIIIC.clean, merged_data$CIIIC.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.6

y_positionROTcomparisonCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.2

#plot of CIIIC
exp1CIIIE <- ggplot(data=ROTresultsfilterCIIIC, aes(x=ROT, y=CIIIC)) + geom_bar(data = summary_statsCIIIC,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIC,
            aes(x = ROT, y = CIIIC, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("D. Complex I+II capacity") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIIIC, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)
```

#Experiment 2
```{r}
ROTresultsfilterCIL <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "two") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIL)) | 
         !(any(TIME == "PRE") & any(is.na(CIL))))

#make each set of data plotted in the right spot
ROTresultsfilterCIL$TIME <- factor(ROTresultsfilterCIL$TIME, levels = c("PRE", "6H"))

#get summary statistics for bars on graph
summary_statsCIL <- ROTresultsfilterCIL %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIL),
            sd_value = sd(CIL, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIL, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIL, ROT == "yes")

# Perform t-test
merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIL.clean, merged_data$CIL.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIL <- max(ROTresultsfilterCIL$CIL) * 1.6

y_positionROTcomparisonCIL <- max(ROTresultsfilterCIL$CIL) * 1.2

#plot of CIL
exp2CIL <-ggplot(data=ROTresultsfilterCIL, aes(x=ROT, y=CIL)) + geom_bar(data = summary_statsCIL,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIL,
            aes(x = ROT, y = CIL, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("A. Complex I leak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIL, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "two") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIP)) | 
         !(any(TIME == "PRE") & any(is.na(CIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIP$TIME <- factor(ROTresultsfilterCIP$TIME, levels = c("PRE", "6H"))

#get summary statistiCIP for bars on graph
summary_statsCIP <- ROTresultsfilterCIP %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIP),
            sd_value = sd(CIP, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIP, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIP, ROT == "yes")

merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIP.clean, merged_data$CIP.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value


#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIP <- max(ROTresultsfilterCIP$CIP) * 1.6

y_positionROTcomparisonCIP <- max(ROTresultsfilterCIP$CIP) * 1.2

#plot of CIP
exp2CIP <- ggplot(data=ROTresultsfilterCIP, aes(x=ROT, y=CIP)) + geom_bar(data = summary_statsCIP,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIP,
            aes(x = ROT, y = CIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("B. Complex I peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIP, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "two") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIIIP)) | 
         !(any(TIME == "PRE") & any(is.na(CIIIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIP$TIME <- factor(ROTresultsfilterCIIIP$TIME, levels = c("PRE", "6H"))

#get summary statistiCIIIP for bars on graph
summary_statsCIIIP <- ROTresultsfilterCIIIP %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIIIP),
            sd_value = sd(CIIIP, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIIIP, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIIIP, ROT == "yes")

merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIIIP.clean, merged_data$CIIIP.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.6

y_positionROTcomparisonCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.2

#plot of CIIIP
exp2CIIIP <- ggplot(data=ROTresultsfilterCIIIP, aes(x=ROT, y=CIIIP)) + geom_bar(data = summary_statsCIIIP,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIP,
            aes(x = ROT, y = CIIIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("C. Complex I+II peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIIIP, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIC <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "two") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIIIC)) | 
         !(any(TIME == "PRE") & any(is.na(CIIIC))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIC$TIME <- factor(ROTresultsfilterCIIIC$TIME, levels = c("PRE", "6H"))

#get summary statistiCIIIC for bars on graph
summary_statsCIIIC <- ROTresultsfilterCIIIC %>%
  group_by(ROT) %>%
  summarise(mean_value = mean(CIIIC),
            sd_value = sd(CIIIC, na.rm = TRUE))

data_clean <- subset(ROTresultsfilterCIIIC, ROT == "no")
data_ROT <- subset(ROTresultsfilterCIIIC, ROT == "yes")

merged_data <- merge(data_clean, data_ROT, by = "ID", suffixes = c(".clean", ".ROT"))
t_test_result <- t.test(merged_data$CIIIC.clean, merged_data$CIIIC.ROT, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.6

y_positionROTcomparisonCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.2

#plot of CIIIC
exp2CIIIE <- ggplot(data=ROTresultsfilterCIIIC, aes(x=ROT, y=CIIIC)) + geom_bar(data = summary_statsCIIIC,
           aes(x=ROT, y = mean_value, fill = ROT),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIC,
            aes(x = ROT, y = CIIIC, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean")) +
  ggtitle("D. Complex I+II capacity") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIIIC, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)
```

#Experiment 3
```{r}
#filter out those who did not complete the measurements
ROTresultsfilterCIL <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "three") %>%
  group_by(ID) %>%
  filter(all(c("PRE", "6H") %in% TIME & !is.na(CIL)) | 
         !(any(TIME == "6H") & any(is.na(CIL))))

#make each set of data plotted in the right spot
ROTresultsfilterCIL$TIME <- factor(ROTresultsfilterCIL$TIME, levels = c("PRE", "6H"))
ROTresultsfilterCIL$interaction_ROT_TIME <- interaction(ROTresultsfilterCIL$ROT, ROTresultsfilterCIL$TIME, drop = TRUE, lex.order = TRUE)

#get summary statistics for bars on graph
summary_statsCIL <- ROTresultsfilterCIL %>%
  group_by(interaction_ROT_TIME) %>%
  summarise(mean_value = mean(CIL),
            sd_value = sd(CIL, na.rm = TRUE))

# Performing two-way ANOVA
anova_resultCIL <- aov(CIL ~ ROT * TIME + Error(ID/ROT), data = ROTresultsfilterCIL)
anovaCIL <- summary(anova_resultCIL)
anovaCIL <- as.data.frame(anovaCIL$`Error: Within`[[1]])
twowaypvalueCIL <- anovaCIL["ROT:TIME", "Pr(>F)"]

#multiple comparisons from two-way ANOVA (-adjust if needed)
multiplecomparisonsCIL <- emmeans(anova_resultCIL, ~ TIME | ROT)
multiplecomparisonsCIL <- summary(pairs(multiplecomparisonsCIL), by = NULL, adjust = "none")

ROTmultiplecomparisons <- multiplecomparisonsCIL$p.value[multiplecomparisonsCIL$ROT == "yes"]
NAmultiplecomparisons <- multiplecomparisonsCIL$p.value[multiplecomparisonsCIL$ROT == "no"]

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIL <- max(ROTresultsfilterCIL$CIL) * 1.6

y_positionROTcomparisonCIL <- max(ROTresultsfilterCIL$CIL[ROTresultsfilterCIL$ROT == "yes"]) * 1.6

y_positionCLEANcomparisonCIL <- max(ROTresultsfilterCIL$CIL[ROTresultsfilterCIL$ROT == "no"]) * 1.6

y_positionanovaCIL <- max(ROTresultsfilterCIL$CIL) * 2.5

#plot of CIL
exp3CIL <- ggplot(data=ROTresultsfilterCIL, aes(x=interaction_ROT_TIME, y=CIL)) + geom_bar(data = summary_statsCIL,
           aes(x=interaction_ROT_TIME, y = mean_value, fill = interaction_ROT_TIME),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no.PRE" = "white", "no.6H" = "white", "yes.PRE" = "lightgrey", "yes.6H" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID,ROT)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIL,
            aes(x = interaction_ROT_TIME, y = CIL, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("no.PRE" = "PRE", "no.6H" = "POST", "yes.PRE"= "PRE", "yes.6H" = "POST")) +
  ggtitle("A. Complex I leak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionCLEANcomparisonCIL, xmin = c(1), xmax = (2),
              annotation =  round(NAmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
   geom_signif(
    y_position = y_positionROTcomparisonCIL, xmin = c(3), xmax = c(4),
   annotation = round(ROTmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
  geom_signif(
              y_position = y_positionanovaCIL, xmin = c(1.5), xmax = (3.5),
              annotation = round(twowaypvalueCIL,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "three") %>%
  group_by(ID) %>%
  filter(all(c("PRE", "6H") %in% TIME & !is.na(CIP)) | 
         !(any(TIME == "6H") & any(is.na(CIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIP$TIME <- factor(ROTresultsfilterCIP$TIME, levels = c("PRE", "6H"))
ROTresultsfilterCIP$interaction_ROT_TIME <- interaction(ROTresultsfilterCIP$ROT, ROTresultsfilterCIP$TIME, drop = TRUE, lex.order = TRUE)

#get summary statistiCIP for bars on graph
summary_statsCIP <- ROTresultsfilterCIP %>%
  group_by(interaction_ROT_TIME) %>%
  summarise(mean_value = mean(CIP),
            sd_value = sd(CIP, na.rm = TRUE))

# Performing two-way ANOVA
anova_resultCIP <- aov(CIP ~ ROT * TIME + Error(ID/ROT), data = ROTresultsfilterCIP)
anovaCIP <- summary(anova_resultCIP)
anovaCIP <- as.data.frame(anovaCIP$`Error: Within`[[1]])
twowaypvalueCIP <- anovaCIP["ROT:TIME", "Pr(>F)"]

#multiple comparisons from two-way ANOVA (-adjust if needed)
multiplecomparisonsCIP <- emmeans(anova_resultCIP, ~ TIME | ROT)
multiplecomparisonsCIP <- summary(pairs(multiplecomparisonsCIP), by = NULL, adjust = "none")

ROTmultiplecomparisons <- multiplecomparisonsCIP$p.value[multiplecomparisonsCIP$ROT == "yes"]
NAmultiplecomparisons <- multiplecomparisonsCIP$p.value[multiplecomparisonsCIP$ROT == "no"]

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIP <- max(ROTresultsfilterCIP$CIP) * 1.6

y_positionROTcomparisonCIP <- max(ROTresultsfilterCIP$CIP[ROTresultsfilterCIP$ROT == "yes"]) * 1.2

y_positionCLEANcomparisonCIP <- max(ROTresultsfilterCIP$CIP[ROTresultsfilterCIP$ROT == "no"]) * 1.2

y_positionanovaCIP <- max(ROTresultsfilterCIP$CIP) * 1.5

#plot of CIP
exp3CIP <-ggplot(data=ROTresultsfilterCIP, aes(x=interaction_ROT_TIME, y=CIP)) + geom_bar(data = summary_statsCIP,
           aes(x=interaction_ROT_TIME, y = mean_value, fill = interaction_ROT_TIME),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no.PRE" = "white", "no.6H" = "white", "yes.PRE" = "lightgrey", "yes.6H" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID,ROT)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIP,
            aes(x = interaction_ROT_TIME, y = CIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("no.PRE" = "PRE", "no.6H" = "POST", "yes.PRE"= "PRE", "yes.6H" = "POST")) +
  ggtitle("B. Complex I peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionCLEANcomparisonCIP, xmin = c(1), xmax = (2),
              annotation =  round(NAmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
   geom_signif(
    y_position = y_positionROTcomparisonCIP, xmin = c(3), xmax = c(4),
   annotation = round(ROTmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
  geom_signif(
              y_position = y_positionanovaCIP, xmin = c(1.5), xmax = (3.5),
              annotation = round(twowaypvalueCIP,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "three") %>%
  group_by(ID) %>%
  filter(all(c("PRE", "6H") %in% TIME & !is.na(CIIIP)) | 
         !(any(TIME == "6H") & any(is.na(CIIIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIP$TIME <- factor(ROTresultsfilterCIIIP$TIME, levels = c("PRE", "6H"))
ROTresultsfilterCIIIP$interaction_ROT_TIME <- interaction(ROTresultsfilterCIIIP$ROT, ROTresultsfilterCIIIP$TIME, drop = TRUE, lex.order = TRUE)

#get summary statistiCIIIP for bars on graph
summary_statsCIIIP <- ROTresultsfilterCIIIP %>%
  group_by(interaction_ROT_TIME) %>%
  summarise(mean_value = mean(CIIIP),
            sd_value = sd(CIIIP, na.rm = TRUE))

# Performing two-way ANOVA
anova_resultCIIIP <- aov(CIIIP ~ ROT * TIME + Error(ID/ROT), data = ROTresultsfilterCIIIP)
anovaCIIIP <- summary(anova_resultCIIIP)
anovaCIIIP <- as.data.frame(anovaCIIIP$`Error: Within`[[1]])
twowaypvalueCIIIP <- anovaCIIIP["ROT:TIME", "Pr(>F)"]

#multiple comparisons from two-way ANOVA (-adjust if needed)
multiplecomparisonsCIIIP <- emmeans(anova_resultCIIIP, ~ TIME | ROT)
multiplecomparisonsCIIIP <- summary(pairs(multiplecomparisonsCIIIP), by = NULL, adjust = "none")

ROTmultiplecomparisons <- multiplecomparisonsCIIIP$p.value[multiplecomparisonsCIIIP$ROT == "yes"]
NAmultiplecomparisons <- multiplecomparisonsCIIIP$p.value[multiplecomparisonsCIIIP$ROT == "no"]

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.6

y_positionROTcomparisonCIIIP <- max(ROTresultsfilterCIIIP$CIIIP[ROTresultsfilterCIIIP$ROT == "yes"]) * 1.2

y_positionCLEANcomparisonCIIIP <- max(ROTresultsfilterCIIIP$CIIIP[ROTresultsfilterCIIIP$ROT == "no"]) * 1.2

y_positionanovaCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.5

#plot of CIIIP
exp3CIIIP <- ggplot(data=ROTresultsfilterCIIIP, aes(x=interaction_ROT_TIME, y=CIIIP)) + geom_bar(data = summary_statsCIIIP,
           aes(x=interaction_ROT_TIME, y = mean_value, fill = interaction_ROT_TIME),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no.PRE" = "white", "no.6H" = "white", "yes.PRE" = "lightgrey", "yes.6H" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID,ROT)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIP,
            aes(x = interaction_ROT_TIME, y = CIIIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("no.PRE" = "PRE", "no.6H" = "POST", "yes.PRE"= "PRE", "yes.6H" = "POST")) +
  ggtitle("C. Complex I+II peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionCLEANcomparisonCIIIP, xmin = c(1), xmax = (2),
              annotation =  round(NAmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
   geom_signif(
    y_position = y_positionROTcomparisonCIIIP, xmin = c(3), xmax = c(4),
   annotation = round(ROTmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
  geom_signif(
              y_position = y_positionanovaCIIIP, xmin = c(1.5), xmax = (3.5),
              annotation = round(twowaypvalueCIIIP,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIC <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "three") %>%
  group_by(ID) %>%
  filter(all(c("PRE", "6H") %in% TIME & !is.na(CIIIC)) | 
         !(any(TIME == "6H") & any(is.na(CIIIC))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIC$TIME <- factor(ROTresultsfilterCIIIC$TIME, levels = c("PRE", "6H"))
ROTresultsfilterCIIIC$interaction_ROT_TIME <- interaction(ROTresultsfilterCIIIC$ROT, ROTresultsfilterCIIIC$TIME, drop = TRUE, lex.order = TRUE)

#get summary statistiCIIIC for bars on graph
summary_statsCIIIC <- ROTresultsfilterCIIIC %>%
  group_by(interaction_ROT_TIME) %>%
  summarise(mean_value = mean(CIIIC),
            sd_value = sd(CIIIC, na.rm = TRUE))

# Performing two-way ANOVA
anova_resultCIIIC <- aov(CIIIC ~ ROT * TIME + Error(ID/ROT), data = ROTresultsfilterCIIIC)
anovaCIIIC <- summary(anova_resultCIIIC)
anovaCIIIC <- as.data.frame(anovaCIIIC$`Error: Within`[[1]])
twowaypvalueCIIIC <- anovaCIIIC["ROT:TIME", "Pr(>F)"]

#multiple comparisons from two-way ANOVA (-adjust if needed)
multiplecomparisonsCIIIC <- emmeans(anova_resultCIIIC, ~ TIME | ROT)
multiplecomparisonsCIIIC <- summary(pairs(multiplecomparisonsCIIIC), by = NULL, adjust = "none")

ROTmultiplecomparisons <- multiplecomparisonsCIIIC$p.value[multiplecomparisonsCIIIC$ROT == "yes"]
NAmultiplecomparisons <- multiplecomparisonsCIIIC$p.value[multiplecomparisonsCIIIC$ROT == "no"]

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.6

y_positionROTcomparisonCIIIC <- max(ROTresultsfilterCIIIC$CIIIC[ROTresultsfilterCIIIC$ROT == "yes"]) * 1.2

y_positionCLEANcomparisonCIIIC <- max(ROTresultsfilterCIIIC$CIIIC[ROTresultsfilterCIIIC$ROT == "no"]) * 1.2

y_positionanovaCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.5

#plot of CIIIC
exp3CIIIE <- ggplot(data=ROTresultsfilterCIIIC, aes(x=interaction_ROT_TIME, y=CIIIC)) + geom_bar(data = summary_statsCIIIC,
           aes(x=interaction_ROT_TIME, y = mean_value, fill = interaction_ROT_TIME),
           stat = "identity",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no.PRE" = "white", "no.6H" = "white", "yes.PRE" = "lightgrey", "yes.6H" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID,ROT)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIC,
            aes(x = interaction_ROT_TIME, y = CIIIC, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("no.PRE" = "PRE", "no.6H" = "POST", "yes.PRE"= "PRE", "yes.6H" = "POST")) +
  ggtitle("D. Complex I+II capacity") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionCLEANcomparisonCIIIC, xmin = c(1), xmax = (2),
              annotation =  round(NAmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
   geom_signif(
    y_position = y_positionROTcomparisonCIIIC, xmin = c(3), xmax = c(4),
   annotation = round(ROTmultiplecomparisons,3), tip_length = 0, family = "sans", size = 1, textsize = 6) +
  geom_signif(
              y_position = y_positionanovaCIIIC, xmin = c(1.5), xmax = (3.5),
              annotation = round(twowaypvalueCIIIC,3), tip_length = 0, family = "sans", size = 1, textsize = 6)
```

#Experiment 4
```{r}
#filter out those who did not complete the measurements
ROTresultsfilterCIL <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "four") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIL)) | 
         !(any(TIME == "PRE") & any(is.na(CIL))))

#make each set of data plotted in the right spot
ROTresultsfilterCIL$TIME <- factor(ROTresultsfilterCIL$TIME, levels = c("PRE", "3H"))

#get summary statistics for bars on graph
summary_statsCIL <- ROTresultsfilterCIL %>%
  group_by(TIME) %>%
  summarise(mean_value = mean(CIL),
            sd_value = sd(CIL, na.rm = TRUE))

data_PRE <- subset(ROTresultsfilterCIL, TIME == "PRE")
data_3H <- subset(ROTresultsfilterCIL, TIME == "3H")

merged_data <- merge(data_PRE, data_3H, by = "ID", suffixes = c(".PRE", ".3H"))
t_test_result <- t.test(merged_data$CIL.PRE, merged_data$CIL.3H, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIL <- max(ROTresultsfilterCIL$CIL) * 1.6

y_positionROTcomparisonCIL <- max(ROTresultsfilterCIL$CIL) * 1.2

#plot of CIL
exp4CIL <- ggplot(data=ROTresultsfilterCIL, aes(x=TIME, y=CIL)) + geom_bar(data = summary_statsCIL,
           aes(x=TIME, y = mean_value),
           stat = "identity",
           fill = "white",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIL,
            aes(x = TIME, y = CIL, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean", "3H" = "POST")) +
  ggtitle("A. ETF + CI leak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIL, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "four") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIP)) | 
         !(any(TIME == "PRE") & any(is.na(CIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIP$TIME <- factor(ROTresultsfilterCIP$TIME, levels = c("PRE", "3H"))

#get summary statistiCIP for bars on graph
summary_statsCIP <- ROTresultsfilterCIP %>%
  group_by(TIME) %>%
  summarise(mean_value = mean(CIP),
            sd_value = sd(CIP, na.rm = TRUE))

data_PRE <- subset(ROTresultsfilterCIP, TIME == "PRE")
data_3H <- subset(ROTresultsfilterCIP, TIME == "3H")

merged_data <- merge(data_PRE, data_3H, by = "ID", suffixes = c(".PRE", ".3H"))
t_test_result <- t.test(merged_data$CIP.PRE, merged_data$CIP.3H, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIP <- max(ROTresultsfilterCIP$CIP) * 1.6

y_positionROTcomparisonCIP <- max(ROTresultsfilterCIP$CIP) * 1.2

#plot of CIP
exp4CIP <- ggplot(data=ROTresultsfilterCIP, aes(x=TIME, y=CIP)) + geom_bar(data = summary_statsCIP,
           aes(x=TIME, y = mean_value),
           stat = "identity",
           fill = "white",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIP,
            aes(x = TIME, y = CIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean", "3H" = "POST")) +
  ggtitle("B. ETF + CI peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIP, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIP <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "four") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIIIP)) | 
         !(any(TIME == "PRE") & any(is.na(CIIIP))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIP$TIME <- factor(ROTresultsfilterCIIIP$TIME, levels = c("PRE", "3H"))

#get summary statistiCIIIP for bars on graph
summary_statsCIIIP <- ROTresultsfilterCIIIP %>%
  group_by(TIME) %>%
  summarise(mean_value = mean(CIIIP),
            sd_value = sd(CIIIP, na.rm = TRUE))

data_PRE <- subset(ROTresultsfilterCIIIP, TIME == "PRE")
data_3H <- subset(ROTresultsfilterCIIIP, TIME == "3H")

merged_data <- merge(data_PRE, data_3H, by = "ID", suffixes = c(".PRE", ".3H"))
t_test_result <- t.test(merged_data$CIIIP.PRE, merged_data$CIIIP.3H, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.6

y_positionROTcomparisonCIIIP <- max(ROTresultsfilterCIIIP$CIIIP) * 1.2

#plot of CIIIP
exp4CIIIP <- ggplot(data=ROTresultsfilterCIIIP, aes(x=TIME, y=CIIIP)) + geom_bar(data = summary_statsCIIIP,
           aes(x=TIME, y = mean_value),
           stat = "identity",
           fill = "white",
           position = "identity",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIP,
            aes(x = TIME, y = CIIIP, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean", "3H" = "POST")) +
  ggtitle("C. ETF+CI+CII peak") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIIIP, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

ROTresultsfilterCIIIC <- ROTresults %>%
  select(-CS) %>% 
  filter(experiment == "four") %>%
  group_by(ID) %>%
  filter(all(c("PRE") %in% TIME & !is.na(CIIIC)) | 
         !(any(TIME == "PRE") & any(is.na(CIIIC))))

#make each set of data plotted in the right spot
ROTresultsfilterCIIIC$TIME <- factor(ROTresultsfilterCIIIC$TIME, levels = c("PRE", "3H"))

#get summary statistiCIIIC for bars on graph
summary_statsCIIIC <- ROTresultsfilterCIIIC %>%
  group_by(TIME) %>%
  summarise(mean_value = mean(CIIIC),
            sd_value = sd(CIIIC, na.rm = TRUE))

data_PRE <- subset(ROTresultsfilterCIIIC, TIME == "PRE")
data_3H <- subset(ROTresultsfilterCIIIC, TIME == "3H")

merged_data <- merge(data_PRE, data_3H, by = "ID", suffixes = c(".PRE", ".3H"))
t_test_result <- t.test(merged_data$CIIIC.PRE, merged_data$CIIIC.3H, paired = TRUE)
ttestpval <- t_test_result$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.5

y_positionROTcomparisonCIIIC <- max(ROTresultsfilterCIIIC$CIIIC) * 1.15

#plot of CIIIC
exp4CIIIE <- ggplot(data=ROTresultsfilterCIIIC, aes(x=TIME, y=CIIIC)) + geom_bar(data = summary_statsCIIIC,
           aes(x=TIME, y = mean_value),
           stat = "identity",
           position = "identity",
           fill = "white",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(aes(group=interaction(ID)), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCIIIC,
            aes(x = TIME, y = CIIIC, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression(atop("Mass-specific mitochondrial respiration", (pmol ~ O[2]*"•"*s^-1*"•mg of wet weight"^-1)))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean", "3H" = "POST")) +
  ggtitle("D. ETF+CI+CII capacity") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 12),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 250), breaks = seq(0, 250, by = 50)) +
    geom_signif(
              y_position = y_positionROTcomparisonCIIIC, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval,3), tip_length = 0, family = "sans", size = 1, textsize = 6)
```

#Fold changes
```{r}
FCrespiration1 <- ROTresults %>%
  dplyr::filter(experiment == "one") %>%
    gather(key = "MEASURE", value = "VALUE", CIP, CIIIP, CIIIC) %>%
  group_by(ID,MEASURE) %>%
  mutate(FC = (((VALUE[ROT == "yes"]) / (VALUE[ROT == "no"])) - 1)*100) %>%
  dplyr::filter(ROT == "yes")

FCrespiration1summary <- FCrespiration1 %>%
  group_by(MEASURE) %>%
  summarise(mean_value = mean(FC, na.rm = TRUE),
            sd_value = sd(FC, na.rm = TRUE))

FCrespiration1$MEASURE <- factor(FCrespiration1$MEASURE, levels = c("CIL", "CIP", "CIIIP", "CIIIC"))

FCexp1 <- ggplot(data=FCrespiration1, aes(x = MEASURE, y = FC)) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black", linewidth = 1) +
  geom_boxplot(color = "black",
               fill = "white",
               width = 0.575,
               linewidth = 1,
               outlier.shape = NA, 
               show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_jitter(aes(x = MEASURE, y = FC, fill = experiment), 
              height = 0, width = 0.15, size = 3, shape = 21, colour = "black", stroke = 1.5, show.legend = FALSE) +
  labs(y = expression("Difference (%)")) +   
 scale_x_discrete(labels = c("CIP" = expression(CI[P]), "CIIIP" = expression(CI+II[P]), "CIIIC" = expression(CI+II[E]))) +
  ggtitle("E. Experiment 1: ROT vs. CLEAN") +
  theme_classic() +
  theme(axis.text.x = element_text(size = 16),
        axis.text.y = element_text(size = 16), 
        axis.title.y = element_text(size = 14),
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title.x = element_blank()) +
  scale_y_continuous(expand = expansion(mult = c(0, 0)), limits = c(-100, 20), breaks = seq(-100, 20, by = 20))

FCrespiration2 <- ROTresults %>%
  dplyr::filter(experiment == "two") %>%
    gather(key = "MEASURE", value = "VALUE", CIP, CIIIP, CIIIC) %>%
  group_by(ID,MEASURE) %>%
  mutate(FC = (((VALUE[ROT == "yes"]) / (VALUE[ROT == "no"])) - 1)*100) %>%
  dplyr::filter(ROT == "yes")

FCrespiration2summary <- FCrespiration2 %>%
  group_by(MEASURE) %>%
  summarise(mean_value = mean(FC, na.rm = TRUE),
            sd_value = sd(FC, na.rm = TRUE))

FCrespiration2$MEASURE <- factor(FCrespiration2$MEASURE, levels = c("CIL", "CIP", "CIIIP", "CIIIC"))

FCexp2 <- ggplot(data=FCrespiration2, aes(x = MEASURE, y = FC)) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "black", linewidth = 1) +
  geom_boxplot(color = "black",
               fill = "white",
               width = 0.575,
               linewidth = 1,
               outlier.shape = NA, 
               show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_jitter(aes(x = MEASURE, y = FC, fill = experiment), 
              height = 0, width = 0.15, size = 3, shape = 21, colour = "black", stroke = 1.5, show.legend = FALSE) +
  labs(y = expression("Difference (%)")) +   
    scale_x_discrete(labels = c("CIP" = expression(CI[P]), "CIIIP" = expression(CI+II[P]), "CIIIC" = expression(CI+II[E]))) +
  ggtitle("E. Experiment 2: ROT vs. CLEAN") +
  theme_classic() +
  theme(axis.text.x = element_text(size = 16),
        axis.text.y = element_text(size = 16), 
        axis.title.y = element_text(size = 14),
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title.x = element_blank()) +
  scale_y_continuous(expand = expansion(mult = c(0, 0)), limits = c(-80, 40), breaks = seq(-80, 40, by = 20))

FCrespiration3 <- ROTresults %>%
  dplyr::filter(experiment == "three") %>%
    gather(key = "MEASURE", value = "VALUE", CIP, CIIIP, CIIIC) %>%
  group_by(ID,MEASURE,ROT) %>%
  mutate(FC = (((VALUE[TIME == "6H"]) / (VALUE[TIME == "PRE"])) - 1)*100) %>%
  dplyr::filter(TIME == "PRE")

FCrespiration3summary <- FCrespiration3 %>%
  group_by(MEASURE,ROT) %>%
  summarise(mean_value = mean(FC, na.rm = TRUE),
            sd_value = sd(FC, na.rm = TRUE))

FCrespiration3$MEASURE <- factor(FCrespiration3$MEASURE, levels = c("CIL", "CIP", "CIIIP", "CIIIC"))

FCexp3 <- ggplot(data=FCrespiration3, aes(x = interaction(ROT,MEASURE), y = FC, fill = ROT)) +
 geom_hline(yintercept = 0, linetype = "dashed", color = "black", linewidth = 1) +
  geom_boxplot(color = "black",
               width = 0.575,
               linewidth = 1,
               outlier.shape = NA, 
               show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_jitter(aes(x = interaction(ROT,MEASURE), y = FC, fill = experiment), 
              height = 0, width = 0.15, size = 3, shape = 21, colour = "black", stroke = 1.5, show.legend = FALSE) +
  labs(y = expression("Difference (%)")) +   
  scale_x_discrete(labels = c("no.CIL" = expression(CI[L]), "yes.CIL" = expression(CI[L]),
                              "no.CIP" = expression(CI[P]), "yes.CIP" = expression(CI[P]),
                              "no.CIIIP" = expression(CI+II[P]), "yes.CIIIP" = expression(CI+II[P]),
                              "no.CIIIC" = expression(CI+II[E]), "yes.CIIIC" = expression(CI+II[E]))) +
  ggtitle("E. Experiment 3: POST vs. PRE") +
  theme_classic() +
  theme(axis.text.x = element_text(size = 13),
        axis.text.y = element_text(size = 16), 
        axis.title.y = element_text(size = 14),
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title.x = element_blank()) +
  scale_y_continuous(expand = expansion(mult = c(0, 0)), limits = c(-60, 60), breaks = seq(-60,60, by = 20))

FCrespiration4 <- ROTresults %>%
  dplyr::filter(experiment == "four") %>%
  gather(key = "MEASURE", value = "VALUE", CIP, CIIIP, CIIIC) %>%
  group_by(ID,MEASURE) %>%
  mutate(FC = (((VALUE[TIME == "3H"]) / (VALUE[TIME == "PRE"])) - 1)*100) %>%
  dplyr::filter(TIME == "PRE")

FCrespiration4summary <- FCrespiration4 %>%
  group_by(MEASURE) %>%
  summarise(mean_value = mean(FC, na.rm = TRUE),
            sd_value = sd(FC, na.rm = TRUE))

FCrespiration4$MEASURE <- factor(FCrespiration4$MEASURE, levels = c("CIL", "CIP", "CIIIP", "CIIIC"))

FCexp4 <- ggplot(data=FCrespiration4, aes(x = MEASURE, y = FC)) + 
 geom_hline(yintercept = 0, linetype = "dashed", color = "black", linewidth = 1) +
  geom_boxplot(color = "black",
               fill = "white",
               width = 0.575,
               linewidth = 1,
               outlier.shape = NA, 
               show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_jitter(aes(x = MEASURE, y = FC, fill = experiment), 
              height = 0, width = 0.15, size = 3, shape = 21, colour = "black", stroke = 1.5, show.legend = FALSE) +
  labs(y = expression("Difference (%)")) +   
  scale_x_discrete(labels = c("CIL" = expression(ETF+CI[L]), "CIP" = expression(ETF+CI[P]), "CIIIP" = expression(ETF+CI+CII[P]), "CIIIC" = expression(ETF+CI+CII[E]), "no.CIL" = expression(ETF+CI[L]), "yes.CIL" = expression(ETF+CI[L]),
                              "no.CIP" = expression(ETF+CI[P]), "yes.CIP" = expression(ETF+CI[P]),
                              "no.CIIIP" = expression(ETF+CI+CII[P]), "yes.CIIIP" = expression(ETF+CI+CII[P]),
                              "no.CIIIC" = expression(ETF+CI+CII[E]), "yes.CIIIC" = expression(ETF+CI+CII[E]))) +
  ggtitle("E. Experiment 4: POST vs. PRE") +
  theme_classic() +
  theme(axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 16), 
        axis.title.y = element_text(size = 14),
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title.x = element_blank()) +
  scale_y_continuous(expand = expansion(mult = c(0, 0)), limits = c(-100, 350), breaks = seq(-100, 350, by = 50))
```

#Citrate synthase
```{r}
ROTresultsfilterCS <- ROTresults %>%
  dplyr::filter(CHAMBERS =="ALL") %>%
  dplyr::filter(!is.na(CS))

#get summary statistiCS for bars on graph
summary_statsCS <- ROTresultsfilterCS %>%
  group_by(experiment,TIME) %>%
  summarise(mean_value = mean(CS),
            sd_value = sd(CS, na.rm = TRUE))

data_PREexercise1 <- ROTresultsfilterCS %>%
  dplyr::filter(experiment == "three") %>%
  dplyr::filter(TIME == "PRE")
data_6Hexercise1 <- ROTresultsfilterCS %>%
  dplyr::filter(TIME == "6H")

data_PREexercise2 <- ROTresultsfilterCS %>%
   dplyr::filter(experiment == "four") %>%
  dplyr::filter(TIME == "PRE")
data_3Hexercise2 <- ROTresultsfilterCS %>%
  dplyr::filter(experiment == "four") %>%
  dplyr::filter(TIME == "3H")

merged_data1 <- merge(data_PREexercise1, data_6Hexercise1, by = "ID", suffixes = c(".PRE", ".6H"))
t_test_result1 <- t.test(merged_data1$CS.PRE, merged_data1$CS.6H, paired = TRUE)
ttestpval1 <- t_test_result1$p.value

merged_data2 <- merge(data_PREexercise2, data_3Hexercise2, by = "ID", suffixes = c(".PRE", ".3H"))
t_test_result2 <- t.test(merged_data2$CS.PRE, merged_data2$CS.3H, paired = TRUE)
ttestpval2 <- t_test_result2$p.value

#personalised positioning of significance bars and graphing elements for each measurement (just makes it easier and consistent)

limitsCS <- max(ROTresultsfilterCS$CS) * 1.6

y_positionROTcomparisonCS <- max(ROTresultsfilterCS$CS) * 1.2

#make each set of data plotted in the right spot
ROTresultsfilterCS$TIME <- factor(ROTresultsfilterCS$TIME, levels = c("PRE", "3H", "6H"))

#plot of CS
CSexp3 <- ggplot(data=ROTresultsfilterCS %>% dplyr::filter(experiment == "three"), aes(x=TIME, y=CS)) + geom_bar(data = summary_statsCS %>% dplyr::filter(experiment == "three"),
           aes(x=TIME, y = mean_value),
           stat = "identity",
           position = "identity",
           fill = "white",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(data=ROTresultsfilterCS %>% dplyr::filter(experiment == "three"), aes(group=ID), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCS %>% dplyr::filter(experiment == "three"),
            aes(x = TIME, y = CS, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
           show.legend = FALSE) +
  labs(y =expression("Citrate synthase activity "(mol*"•"*h^-1*"•"*"kg protein"^-1))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean", "PRE" = "PRE", "6H" = "POST", "3H" = "POST")) +
  ggtitle("F. Experiment 3: CS activity") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 10), breaks = seq(0, 10, by = 2)) +
    geom_signif(
              y_position = y_positionROTcomparisonCS, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval2,3), tip_length = 0, family = "sans", size = 1, textsize = 6)

CSexp4 <- ggplot(data=ROTresultsfilterCS %>% dplyr::filter(experiment == "four"), aes(x=TIME, y=CS)) + geom_bar(data = summary_statsCS %>% dplyr::filter(experiment == "four"),
           aes(x=TIME, y = mean_value),
           stat = "identity",
           position = "identity",
           fill = "white",
           color = "black",
           width = 0.5,
           linewidth = 1,
           show.legend = FALSE) +
  scale_fill_manual(values = c("no" = "white", "yes" = "lightgrey", "one" = "#E41A1C", "two" = "#377EB8", "three" = "#984EA3", "four" = "#4DAF4A"),
                    name = "Presence of ROT",
                    labels = c("No", "Yes")) +
  geom_line(data=ROTresultsfilterCS %>% dplyr::filter(experiment == "four"), aes(group=ID), colour = "black", size = 0.5) +
  geom_jitter(data = ROTresultsfilterCS %>% dplyr::filter(experiment == "four"),
            aes(x = TIME, y = CS, fill = experiment), height = 0, width = 0,
      
             size = 3,
            shape = 21,
             colour = "black",
            stroke = 1.5,
             show.legend = FALSE) +
  labs(y =expression("Citrate synthase activity "(mol*"•"*h^-1*"•"*"kg protein"^-1))) +   
  scale_x_discrete(labels = c("yes" = "ROT", "no" = "Clean", "PRE" = "PRE", "6H" = "POST", "3H" = "POST")) +
  ggtitle("F. Experiment 4: CS activity") +
  theme_classic() +
   theme(axis.text.x = element_text(size = 12,),
        axis.text.y = element_text(size = 12), 
        axis.title.y = element_text(size = 10),
        plot.title = element_text(size = 16, hjust=0.45),
        axis.title.x = element_blank()) +
scale_y_continuous(expand = expansion(mult = c(0, 0)), limits= c(0, 10), breaks = seq(0, 10, by = 2)) +
    geom_signif(
              y_position = y_positionROTcomparisonCS, xmin = c(1), xmax = (2),
              annotation =  round(ttestpval1,3), tip_length = 0, family = "sans", size = 1, textsize = 6)
```

#Export figures
```{r}
Exp1 <- arrangeGrob(exp1CIL, exp1CIP, FCexp1, exp1CIIIP, exp1CIIIE, legendsex, 
                    ncol = 3, widths = c(3, 3, 3.2))

svg("Experiment1.svg", width = 12, height = 8)
grid.draw(Exp1)
dev.off()

Exp2 <- arrangeGrob(exp2CIL, exp2CIP, FCexp2, exp2CIIIP, exp2CIIIE,legendsex, ncol = 3, widths=c(3,3,3))

svg("Experiment2.svg", width = 12, height = 8)
grid.draw(Exp2)
dev.off()

combined_grobexp3 <- arrangeGrob(CSexp3, legendsex, ncol = 2)

Exp3 <- grid.arrange(exp3CIL, exp3CIP, FCexp3, exp3CIIIP, exp3CIIIE, combined_grobexp3, ncol = 3, widths=c(3,3,4))

svg("Experiment3.svg", width = 12, height = 8)
grid.draw(Exp3)
dev.off()

combined_grobexp4 <- arrangeGrob(CSexp4, legendsex2, ncol = 2)

Exp4 <- grid.arrange(exp4CIL, exp4CIP, FCexp4, exp4CIIIP, exp4CIIIE, combined_grobexp4, ncol = 3, widths=c(3,3,4))

svg("Experiment4.svg", width = 12, height = 8)
grid.draw(Exp4)
dev.off()
```

