knitr::opts_chunk$set(echo = TRUE, 
                      warning = FALSE,
                      error = FALSE,
                      message = FALSE,
                      include = TRUE,
                      fig.width = 11,
                      fig.height = 5)
if(!require("rprojroot")) install.packages("rprojroot", dependencies = TRUE)
library(rprojroot)
root <- rprojroot::find_root_file(criterion = is_rstudio_project)
root
# install.packages("gdata")
# install.packages("stargazer")
# install.packages("arm")
# library(arm)
# library(stargazer)
# library(gdata)
# library(RCurl)
# install.packages("svglite")
library(tidyverse)
library(purrr)
library(readxl)
library(readr)
library(svglite)
library(ggplot2)
library(gridExtra)
library(grid)
library(citrulliner)

Data

Data load

library(citrulliner)
data <- citrulliner::citrulline_data

Inpect data

head(data)
levels(data$subject)
levels(data$protocol)
levels(data$time)
levels(data$analyte %>% as.factor())

Select and rename Variables

names(data)
data <- data %>%
  dplyr::select(subject,
         protocol,
         time,
         analyte,
         concentration,
         duplicated
         )

Summarize data

The number of missing values is r sum(is.na(data))

names(data)
data_summary <- data %>%
#  na.omit() %>%
   dplyr::filter(duplicated == "FALSE") %>%
   dplyr::group_by(protocol,
           time,
           analyte) %>%

  dplyr::summarise(mean_conc = round(mean(concentration), digits = 4),
            sd = round(sd(concentration), digits = 3),
            n_obs = n(),
            sem = round(sd/sqrt(n_obs), digits = 3)
            ) %>%
  dplyr::arrange(protocol, analyte)

#names(diagrams_summary)[1] <- c("protocol")
#names(diagrams_summary)[2] <- c("time")
data_summary

Split for Analytes; purrr::nest()

## split for analytes
data_by_analyte <- data_summary %>%
#  as_tibble() %>%
#  gather(exam:numeracy, key = "measure", value = "value") %>%
  group_by(analyte) %>%
  nest() %>% print()

data_by_analyte$data[[1]]

Split by analyte; split()

data_summary$analyte <- as.factor(data_summary$analyte)
levels(data_summary$analyte)

data_split <- split(data_summary, 
                        droplevels(data_summary$analyte))

Set colours

RColorBrewer::display.brewer.all()
palette <- RColorBrewer::brewer.pal(7, "Set1")

# GetColorHexAndDecimal <- function(color){
#   c <- col2rgb(color)
#   
#   sprintf("#%02X%02X%02X%3d%3d%3d", c[1],c[2],c[3],c[1],c[2],c[3])
# } 
# 
# black <- GetColorHexAndDecimal("black")
palette <- c("#000000", palette[c(1:3,4)])

All lines, individually

# define image directory
image_directory <- file.path(root, "inst", "images")
dir.create(file.path(root, "inst",
                           "images"))

# argument for function test
analyte = "ala"

print_lines <- function(analyte, ...){

  #ymax_data <- max(data_split[[analyte]]$mean_conc)
#  sem_max <- max(data_split[[analyte]]$sem)
#  ymax <- 1.6*(ymax_data)

#  ymin_data <- min(data_split[[analyte]]$mean_conc)
#  ymin <- ymin_data - 1.6*(ymin_data)

data_plot <- data_split[[analyte]]


 plot <- citrulliner::draw_lines(DF = data_plot, 
                                 palette_graph = palette,
                                # ymax = ymax,
                                # ymin = ymin 
                                f.width = 14, f.height = 10) 




  return(plot)
}

ala_lines <- print_lines(analyte = "ala")
ala_lines
plot_list <- lapply(levels(data_summary$analyte), print_lines)
names(plot_list) <- levels(data_summary$analyte)
names(plot_list)

print_lines("ala", f.width = 18, f.height = 8)

Panel plots for the paper

library(profvis)

## get profile; for speed optimization, omitting saving as png can improve speed in this case 
# profvis::profvis({

library(citrulliner)
library(gtable)
library(cowplot)

print_lines_panel <- function(analyte){

  ymax_data <- max(data_split[[analyte]]$mean_conc)
  sem_max <- max(data_split[[analyte]]$sem)
  ymax <- 1.05*(ymax_data)

  ymin_data <- min(data_split[[analyte]]$mean_conc)
  ymin <- ymin_data - (0.05*ymin_data)

data_plot <- data_split[[analyte]]


 plot <- citrulliner::draw_lines_panel(DF = data_plot, 
                                       palette_graph = palette, 
                                       ymax = ymax,
                                       ymin = ymin)



  return(plot)
}


panel_plot_list <- lapply(levels(data_summary$analyte), print_lines_panel)
names(panel_plot_list) <- levels(data_summary$analyte)
names(panel_plot_list)

panel_plot_list[[1]]


#})

Select figures for panels

# figure 3; panel
p3_a <- panel_plot_list[["gln"]]
p3_b <- panel_plot_list[["ala"]]
p3_c <- panel_plot_list[["citrul"]]
p3_d <- panel_plot_list[["arg"]]
p3_e <- panel_plot_list[["UREUM"]]
p3_f <- panel_plot_list[["CK"]]
# figure 4; panel
p4_a <- panel_plot_list[["ifabp"]]
p4_b <- panel_plot_list[["CORT"]]
# figure rubuttal
rebuttal_a <- panel_plot_list[["citrul"]]
rebuttal_b <- panel_plot_list[["CK"]]

Panels

Figure 3

# https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html

figure_3 <- plot_grid( p3_a + theme(legend.position="none"),
           p3_b + theme(legend.position="none"),
           p3_c + theme(legend.position="none"),
           p3_d + theme(legend.position="none"),
           p3_e + theme(legend.position="none"),
           p3_f + theme(legend.position="none"),
           align = 'vh',
           labels = c("A", "B", "C", "D", "E", "F"),
           hjust = -8,
           ncol = 2,
           nrow = 3,
           label_size = 18
           )

figure_3
legend_b <- get_legend(p3_a + theme(legend.position="bottom"))

p_3 <- plot_grid(legend_b, figure_3, ncol = 1, rel_heights = c(0.05, 1))

## citrulliner::save_in_image_directory(filename = "test2.svg", height = 26, width = 7)

Figure "Rubuttal - Citrulline vs. Creatine phosphokinase

rebuttal <- plot_grid(rebuttal_a + theme(legend.position = "none"),
           rebuttal_b + theme(legend.position = "none"),
           align = 'vh',
           labels = c("A", "B"),
           hjust = -6,
           ncol = 1,
           nrow = 2,
           label_size = 18
           )



legend_b <- get_legend(rebuttal_a + theme(legend.position = "bottom"))

rebuttal <- plot_grid(legend_b, rebuttal, ncol = 1, rel_heights = c(0.1, 1))

## citrulliner::save_in_image_directory(filename = "test2.svg", height = 26, width = 7)

Fig 3 - Saving to disk

svg(filename = "./inst/images/panel_fig_3.svg", 
    height = 20, width = 20)
p_3
dev.off()

pdf(file = "./inst/images/panel_fig_3.pdf", height = 20, 
    width = 20)
p_3
dev.off()



p_3

Fig 4 - Saving to disk

svg(filename = "./inst/images/panel_fig_4.svg", height = 14, width = 10)
p_4
dev.off()

pdf(file = "./inst/images/panel_fig_4.pdf", height = 14, width = 10)
p_4
dev.off()

p_4

Fig rubuttal - Saving to disk

svg(filename = "./inst/images/fig_rebuttal.svg", height = 14, width = 10)
rebuttal
dev.off()

pdf(file = "./inst/images/fig_rebuttal.pdf", height = 14, width = 10)
rebuttal 
dev.off()

png(file = "./inst/images/fig_rebuttal.png", height = 40, width = 25, units = "cm", res = 300)
rebuttal 
dev.off()

Kinetics paper



uashogeschoolutrecht/citrulliner documentation built on Dec. 12, 2020, 7:12 a.m.