Clean up the environment

rm(list = ls())

Load library

library(readxl)       # For reading Excel files
library(ggpubr)       # For creating publication-ready plots
library(dplyr)        # For data manipulation
library(tidyr)        # For reshaping data, replacing melt() from reshape2
library(ggh4x)        # For advanced faceting functions in ggplot2
library(rstatix)      # For statistical tests and adding significance markers
library(knitr)        # For displaying tibbles in a nice table format
library(kableExtra)   # For enhancing table formatting

Load data

# Load targeted serum and liver data
Serum <- read_excel("Data/Data.xlsx", sheet = "Nec_serum_targeted")
Liver <- read_excel("Data/Data.xlsx", sheet = "Liver_targeted")

# Load pig information and necropsy metadata
PigInfo <- read_excel("Data/Data.xlsx", sheet = "PigInfo")
Nec_metadata <- read_excel("Data/Data.xlsx", sheet = "Nec_metadata")

Data organization

# Define the list of metabolites for each sample type
ls.serum.metabolites <- c("3-Hydroxybutyrate", "Acetoacetate")
ls.liver.metabolites <- c("3-Hydroxybutyrate")
Data.serum <- inner_join(Serum, PigInfo, by = "PigID") %>%
                select(Diet, all_of(ls.serum.metabolites)) %>%
                mutate(Region = "Serum") %>%
                pivot_longer(cols = all_of(ls.serum.metabolites), names_to = "variable", values_to = "value")


Data.liver <- inner_join(Liver, PigInfo, by = "PigID") %>%
                select(Diet, all_of(ls.liver.metabolites)) %>%
                mutate(Region = "Liver") %>%
                pivot_longer(cols = all_of(ls.liver.metabolites), names_to = "variable", values_to = "value")
# Combine serum and liver data into a single dataset
All.data <- bind_rows(Data.serum, Data.liver) %>%
              mutate(Region = factor(Region, levels = c("Serum", "Liver")))

Plotting data

Fig <- All.data %>% 
        mutate(Diet = factor(Diet, levels = c("ALAC", "WPI", "SF"))) %>%
        ggerrorplot(x = "variable", y = "value", color = "Diet", 
            palette = c("turquoise3", "purple", "orange"),
            na.rm = TRUE, 
            xlab = "", 
            ylab = "Concentration", 
            add = "mean_se", 
            size = 0.6,
            position = position_dodge(0.7)) +
            facet_nested(~Region + variable, scales = "free", independent = "y") +  # Free x axis with proportional facet widths using facet_nested
            theme_classic2() +
            theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())


Fig

# Save the figure in pdf format:
ggsave(plot=Fig, height=4, width=6, dpi=300, filename="Figure 5/Fig5B.pdf", useDingbats=FALSE)

Group difference evaluation

#This function transforms the input values by the generalized #log function.
glog <- function(y) {
  #Using lambda = 1 
  yt <- log(y+sqrt(y^2+1))
  return(yt)
}

##ALAC vs WPI: Serum and liver data, adjusting for intake volume and postprandial time

Serum %>% inner_join(PigInfo, by = "PigID") %>%
            inner_join(Nec_metadata, by = "PigID") %>%
            select(`3-Hydroxybutyrate`, `Acetoacetate`, Diet, SowID, Formula.consumed.g, Time.postprandial.min) %>%
            pivot_longer(cols = c(`3-Hydroxybutyrate`, `Acetoacetate`), names_to = "variable", values_to = "value") %>%
            filter(Diet %in% c("ALAC", "WPI")) %>%
            group_by(variable) %>%
            mutate(glog.value = glog(value * 10)) %>% # Apply generalized log transformation to metabolite values
            anova_test(glog.value ~ Diet + SowID + Formula.consumed.g + Time.postprandial.min, type = 1) %>% # Perform ANOVA
            add_significance(p.col = "p", cutpoints = c(0, 0.05, 0.1, 1), symbols = c("*", "#", "ns")) %>% # Add significance markers
            as_tibble() %>%
            filter(Effect == "Diet") %>%
            kable() %>% # Print the tibble in a nicely formatted table
            kable_styling(bootstrap_options = c("striped", "hover")) # Enhance table formatting with kableExtra
variable Effect DFn DFd F p p<.05 ges p.signif
3-Hydroxybutyrate Diet 1 17 16.933 0.000723
0.499
Acetoacetate Diet 1 17 3.073 0.098000 0.153 #
Liver %>% inner_join(PigInfo, by = "PigID") %>%
            inner_join(Nec_metadata, by = "PigID") %>% 
            filter(Diet %in% c("ALAC", "WPI")) %>%
            mutate(glog.3HB = glog(`3-Hydroxybutyrate` * 10)) %>% # glog transformation
            anova_test(glog.3HB ~ Diet + SowID + Formula.consumed.g + Time.postprandial.min, type = 1) 
## ANOVA Table (type I tests)
## 
##                  Effect DFn DFd      F        p p<.05   ges
## 1                  Diet   1  17  8.665 9.00e-03     * 0.338
## 2                 SowID   2  17 17.317 7.92e-05     * 0.671
## 3    Formula.consumed.g   1  17  2.581 1.27e-01       0.132
## 4 Time.postprandial.min   1  17  1.582 2.26e-01       0.085

Between formula-fed groups and SF, adjusting for sow ID

Serum %>% inner_join(PigInfo, by = "PigID") %>%
            inner_join(Nec_metadata, by = "PigID") %>%
            select(`3-Hydroxybutyrate`, `Acetoacetate`, Diet, SowID) %>%
            pivot_longer(cols = c(`3-Hydroxybutyrate`, `Acetoacetate`), names_to = "variable", values_to = "value") %>%
            group_by(variable) %>%
            mutate(glog.value = glog(value * 10)) %>% # Apply generalized log transformation to metabolite values
            tukey_hsd(glog.value ~ Diet + SowID) %>% # Perform Tukey HSD test
            filter(term == "Diet") %>%
            add_significance(p.col = "p.adj", cutpoints = c(0, 0.05, 0.1, 1), symbols = c("*", "#", "ns")) %>%
            kable() %>% # Print the tibble in a nice table format
            kable_styling(bootstrap_options = c("striped", "hover")) # Enhance table formatting
variable term group1 group2 null.value estimate conf.low conf.high p.adj p.adj.signif
3-Hydroxybutyrate Diet ALAC SF 0 -1.4876968 -2.2638967 -0.7114968 0.000204
3-Hydroxybutyrate Diet ALAC WPI 0 -0.7500827 -1.3884903 -0.1116751 0.019100
3-Hydroxybutyrate Diet SF WPI 0 0.7376140 -0.0270860 1.5023141 0.060100 #
Acetoacetate Diet ALAC SF 0 -1.1346856 -1.8158411 -0.4535301 0.000989
Acetoacetate Diet ALAC WPI 0 -0.3770512 -0.9372868 0.1831845 0.233000 ns
Acetoacetate Diet SF WPI 0 0.7576344 0.0865706 1.4286982 0.024800
Liver %>% inner_join(PigInfo, by = "PigID") %>%
            inner_join(Nec_metadata, by = "PigID") %>%
            select(`3-Hydroxybutyrate`, Diet, SowID) %>%
            pivot_longer(cols = c(`3-Hydroxybutyrate`), names_to = "variable", values_to = "value") %>%
            group_by(variable) %>%
            mutate(glog.value = glog(value * 10)) %>%
            tukey_hsd(glog.value ~ Diet + SowID) %>%
            filter(term == "Diet") %>%
            add_significance(p.col = "p.adj", cutpoints = c(0, 0.05, 0.1, 1), symbols = c("*", "#", "ns")) %>%
            kable() %>% # Print the tibble in a nice table format
            kable_styling(bootstrap_options = c("striped", "hover")) # Enhance table formatting
variable term group1 group2 null.value estimate conf.low conf.high p.adj p.adj.signif
3-Hydroxybutyrate Diet ALAC SF 0 -4.4165417 -6.707699 -2.125384 0.000191
3-Hydroxybutyrate Diet ALAC WPI 0 -0.5942223 -2.478650 1.290205 0.714000 ns
3-Hydroxybutyrate Diet SF WPI 0 3.8223194 1.565107 6.079532 0.000832
sessionInfo()
## R version 4.2.2 (2022-10-31)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur ... 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] kableExtra_1.3.4 knitr_1.45       rstatix_0.7.2    ggh4x_0.2.6     
## [5] tidyr_1.3.0      dplyr_1.1.3      ggpubr_0.6.0     ggplot2_3.5.1   
## [9] readxl_1.4.3    
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.0  xfun_0.40         bslib_0.5.1       purrr_1.0.2      
##  [5] carData_3.0-5     colorspace_2.1-0  vctrs_0.6.5       generics_0.1.3   
##  [9] viridisLite_0.4.2 htmltools_0.5.6.1 yaml_2.3.7        utf8_1.2.4       
## [13] rlang_1.1.2       jquerylib_0.1.4   pillar_1.9.0      glue_1.6.2       
## [17] withr_3.0.1       lifecycle_1.0.4   stringr_1.5.1     munsell_0.5.1    
## [21] ggsignif_0.6.4    gtable_0.3.5      cellranger_1.1.0  ragg_1.2.6       
## [25] rvest_1.0.3       evaluate_1.0.1    labeling_0.4.3    fastmap_1.1.1    
## [29] fansi_1.0.6       highr_0.10        broom_1.0.5       scales_1.3.0     
## [33] backports_1.4.1   cachem_1.0.8      webshot_0.5.5     jsonlite_1.8.8   
## [37] abind_1.4-5       farver_2.1.1      systemfonts_1.0.5 textshaping_0.3.7
## [41] digest_0.6.33     stringi_1.7.12    grid_4.2.2        cli_3.6.2        
## [45] tools_4.2.2       magrittr_2.0.3    sass_0.4.7        tibble_3.2.1     
## [49] car_3.1-2         pkgconfig_2.0.3   xml2_1.3.5        rmarkdown_2.28   
## [53] svglite_2.1.2     httr_1.4.7        rstudioapi_0.15.0 R6_2.5.1         
## [57] compiler_4.2.2