|
| 1 | +#' Automatically save boxplots for all metabolites including asterisks for significance. |
| 2 | +#' |
| 3 | +#' @param data List inheriting from Allstats |
| 4 | +#' @param asterisk Choose asterisk to plot ("Dunn","Scheffe","u_test","t_test") |
| 5 | +#' @param significant_variable_only show significant variable only (T,F) |
| 6 | +#' @param color colors used for ggplots. |
| 7 | +#' @param legend_position legend position "none","left","right","bottom","top" |
| 8 | +#' @param order order of the groups; order = c("A","B","C") |
| 9 | +#' @param tip_length significance tip length |
| 10 | +#' @param label_size significance label size |
| 11 | +#' @param step_increase significance step increase |
| 12 | +#' @param width box width |
| 13 | +#' @param fig_width figure size |
| 14 | +#' @param fig_height figure size |
| 15 | +#' |
| 16 | +#' @return ggboxplot |
| 17 | +#' @export |
| 18 | +#' |
| 19 | +#' @examples D<-as.data.frame(cbind(LETTERS,rep(1,26),c(rep("A",7),rep("B",6),rep("C",13)),runif(26),runif(26))) |
| 20 | +#' Datas<-rbind(c("Sample","Multilevel","Group","some metabolite1","some metabolite2"),D) |
| 21 | +#' Test<-Allstats(Datas) |
| 22 | +#' AS_boxplot(Test,asterisk = "Dunn",significant_variable_only = F,color = c("#FF3300","#FF6600","#FFCC00","#99CC00","#0066CC","#660099") ) |
| 23 | +AS_boxplot<-function(data, |
| 24 | + asterisk="t_test", |
| 25 | + significant_variable_only=F, |
| 26 | + color=c("#FF3300","#FF6600","#FFCC00","#99CC00","#0066CC","#660099"), |
| 27 | + legend_position = "none", |
| 28 | + order = NULL, |
| 29 | + tip_length = 0.01, |
| 30 | + label_size = 2.88, |
| 31 | + step_increase = 0.05, |
| 32 | + width = 0.3, |
| 33 | + fig_width = NA, |
| 34 | + fig_height = NA){ |
| 35 | + {#Summary |
| 36 | + ###Plot_data_prep### |
| 37 | + ifelse(!dir.exists(file.path(getwd(), "barplot")), dir.create(file.path(getwd(), "boxplot")), FALSE) |
| 38 | + data[["Data_renamed"]]<-data[["Data_renamed"]] %>% plyr::mutate( |
| 39 | + ZZZZ =data[["Data_renamed"]][,2] ) |
| 40 | + data[["Data_renamed"]]<-data[["Data_renamed"]][,c(-1,-2)] |
| 41 | + data[["Data_renamed"]][,1:(ncol(data[["Data_renamed"]])-1)]<-sapply(data[["Data_renamed"]][,1:(ncol(data[["Data_renamed"]])-1)], |
| 42 | + function(x) as.numeric(x)) |
| 43 | + NAMES<-colnames(data[["Data"]][3:ncol(data[["Data"]])]) |
| 44 | + data[["Data_renamed"]]$ZZZZ<-as.factor(data[["Data_renamed"]]$ZZZZ) |
| 45 | + colnames(data[["Data_renamed"]])[ncol(data[["Data_renamed"]])]<-"Group" |
| 46 | + Comb<-gtools::combinations(length(unique(data[["Data"]]$Group)),2,unique(data[["Data"]]$Group)) |
| 47 | + ckey<-color[1:length(unique(data[["Data"]]$Group))] |
| 48 | + |
| 49 | + if (asterisk =="Scheffe"){ |
| 50 | + p_val_data<-data[["Anova_PostHoc"]]} |
| 51 | + else if (asterisk =="t_test"){ |
| 52 | + p_val_data<-data[["t_test"]]} |
| 53 | + else if (asterisk =="u_test"){ |
| 54 | + p_val_data<-data[["u_test"]]} |
| 55 | + else if (asterisk =="Dunn"){ |
| 56 | + p_val_data<-data[["Dunn"]]} |
| 57 | + else { |
| 58 | + print("Wrong asterisk input must be one of (Dunn,Scheffe,u_test,t_test)")}} |
| 59 | + ###Plots### |
| 60 | + suppressWarnings( |
| 61 | + if (length(unique(data[["Data_renamed"]][["Group"]]))!=2) { |
| 62 | + for (number in 1:nrow(p_val_data)){ |
| 63 | + if (number == round(nrow(p_val_data)/4)){print("25% complete")} |
| 64 | + if (number == round(nrow(p_val_data)/3)){print("33% complete")} |
| 65 | + if (number == round(nrow(p_val_data)/2)){print("50% complete")} |
| 66 | + if (number == round(2*nrow(p_val_data)/3)){print("66% complete")} |
| 67 | + if (number == round(3*nrow(p_val_data)/4)){print("75% complete")} |
| 68 | + if (number == nrow(p_val_data)){print("100% complete")} |
| 69 | + stat.test <- as.data.frame(matrix(data = NA, nrow = nrow(Comb), |
| 70 | + ncol = 2)) |
| 71 | + stat.test[, 1:2] <- Comb |
| 72 | + if (asterisk == "Dunn"){ |
| 73 | + rownames(stat.test)<-paste0(stat.test[,1]," - ",stat.test[,2])} |
| 74 | + else{ |
| 75 | + rownames(stat.test)<-paste0(stat.test[,1],"-",stat.test[,2])} |
| 76 | + colnames(p_val_data)<-as.data.frame(strsplit(colnames(p_val_data),"___"))[1,] |
| 77 | + t1<-as.data.frame(t(as.data.frame(p_val_data)[number,])) |
| 78 | + t2<-stat.test |
| 79 | + t3<-merge(t2,t1,by=0) |
| 80 | + stat.test<-t3[,2:4] |
| 81 | + colnames(stat.test) <- c("group1", "group2", |
| 82 | + "p") |
| 83 | + stat.test<-stat.test %>% plyr::mutate( |
| 84 | + p.adj.signif = case_when( |
| 85 | + p > 0.05 ~ 'NS', |
| 86 | + p <= 0.05 &p > 0.01 ~ '*', |
| 87 | + p <= 0.01 ~ '**' |
| 88 | + ) |
| 89 | + ) |
| 90 | + stat.test<-stat.test[stat.test$p.adj.signif != "NS",] |
| 91 | + if (length(stat.test>4)){ |
| 92 | + ggpubr::ggboxplot(data[["Data_renamed"]], |
| 93 | + x = "Group", |
| 94 | + y = colnames(data[["Data_renamed"]])[number], |
| 95 | + color = "Group", |
| 96 | + palette = ckey, |
| 97 | + add = "jitter", |
| 98 | + order = order, |
| 99 | + width = width)+ |
| 100 | + scale_y_continuous(label=ecoflux::scientific_10x)+ |
| 101 | + labs(title=NAMES[number],x =NULL, y = "Intensity")+ |
| 102 | + theme_classic()+ ggplot2::theme(plot.title=element_text(size=15, |
| 103 | + face="bold", |
| 104 | + color="Black", |
| 105 | + hjust=0.5, |
| 106 | + lineheight=1.2), # title |
| 107 | + plot.subtitle=element_text(size=15, |
| 108 | + hjust=0.5), # subtitle |
| 109 | + plot.caption=element_text(size=15), # caption |
| 110 | + axis.title.x=element_text(vjust=10, |
| 111 | + size=15), # X axis title |
| 112 | + axis.title.y=element_text(size=12), # Y axis title |
| 113 | + axis.text.x=element_text(size=10, |
| 114 | + vjust=.5), # X axis text |
| 115 | + axis.text.y=element_text(size=10), |
| 116 | + legend.position = legend_position)+ |
| 117 | + ggpubr::stat_pvalue_manual(stat.test, |
| 118 | + y.position = 1.05 * max(data[["Data_renamed"]][,number]), |
| 119 | + step.increase = step_increase, |
| 120 | + label.size = label_size, |
| 121 | + tip.length = tip_length, |
| 122 | + label = "p.adj.signif", |
| 123 | + size = 3.5, |
| 124 | + vjust = 0.05) |
| 125 | + ggsave(filename = paste(NAMES[number],"boxplot.png",collapse = ""), |
| 126 | + path=paste0(getwd(),"/boxplot"), |
| 127 | + width = fig_width, |
| 128 | + height = fig_height)} |
| 129 | + else if(significant_variable_only == F) { |
| 130 | + ggpubr::ggboxplot(data[["Data_renamed"]], |
| 131 | + x = "Group", |
| 132 | + y = colnames(data[["Data_renamed"]])[number], |
| 133 | + color = "Group", |
| 134 | + palette = ckey, |
| 135 | + add = "jitter", |
| 136 | + order = order, |
| 137 | + width = width)+ |
| 138 | + scale_y_continuous(label=ecoflux::scientific_10x)+ |
| 139 | + labs(title=NAMES[number],x =NULL, y = "Intensity")+ |
| 140 | + theme_classic()+ ggplot2::theme(plot.title=element_text(size=15, |
| 141 | + face="bold", |
| 142 | + color="Black", |
| 143 | + hjust=0.5, |
| 144 | + lineheight=1.2), # title |
| 145 | + plot.subtitle=element_text(size=15, |
| 146 | + hjust=0.5), # subtitle |
| 147 | + plot.caption=element_text(size=15), # caption |
| 148 | + axis.title.x=element_text(vjust=10, |
| 149 | + size=15), # X axis title |
| 150 | + axis.title.y=element_text(size=12), # Y axis title |
| 151 | + axis.text.x=element_text(size=10, |
| 152 | + vjust=.5), # X axis text |
| 153 | + axis.text.y=element_text(size=10), |
| 154 | + legend.position = legend_position) |
| 155 | + |
| 156 | + ggsave(filename = paste(NAMES[number],"boxplot.png",collapse = ""), |
| 157 | + path=paste0(getwd(),"/boxplot"), |
| 158 | + width = fig_width, |
| 159 | + height = fig_height)}}} |
| 160 | + else if (length(unique(data[["Data_renamed"]][["Group"]]))==2) { |
| 161 | + p_val_data<-as.data.frame(p_val_data) |
| 162 | + if (asterisk=="t_test"){ |
| 163 | + colnames(p_val_data)<-colnames(data[["Result"]])[1] |
| 164 | + } |
| 165 | + else if(asterisk == "u_test"){ |
| 166 | + colnames(p_val_data)<-colnames(data[["Result"]])[2] |
| 167 | + } |
| 168 | + |
| 169 | + for (number in 1:nrow(p_val_data)){ |
| 170 | + if (number == round(nrow(p_val_data)/4)){print("25% complete")} |
| 171 | + if (number == round(nrow(p_val_data)/3)){print("33% complete")} |
| 172 | + if (number == round(nrow(p_val_data)/2)){print("50% complete")} |
| 173 | + if (number == round(2*nrow(p_val_data)/3)){print("66% complete")} |
| 174 | + if (number == round(3*nrow(p_val_data)/4)){print("75% complete")} |
| 175 | + if (number == nrow(p_val_data)){print("100% complete")} |
| 176 | + df <- data_summary(data[["Data_renamed"]], |
| 177 | + varname = colnames(data[["Data_renamed"]])[number], |
| 178 | + groupnames = c("Group")) |
| 179 | + df <- df %>% plyr::mutate(ymax = len + SEM) |
| 180 | + colnames(df)[1] <- "Group" |
| 181 | + stat.test <- as.data.frame(matrix(data = NA, nrow = nrow(Comb), |
| 182 | + ncol = 2)) |
| 183 | + stat.test[, 1:2] <- Comb |
| 184 | + if (asterisk == "Dunn") { |
| 185 | + rownames(stat.test) <- paste0(stat.test[, 1], |
| 186 | + " - ", stat.test[, 2]) |
| 187 | + } |
| 188 | + else { |
| 189 | + rownames(stat.test) <- paste0(stat.test[, 1], |
| 190 | + "-", stat.test[, 2]) |
| 191 | + } |
| 192 | + colnames(p_val_data) <- as.data.frame(strsplit(colnames(p_val_data), |
| 193 | + "___"))[1, ] |
| 194 | + t1 <- as.data.frame(t(as.data.frame(p_val_data[number, |
| 195 | + ]))) |
| 196 | + rownames(t1)<-colnames(p_val_data) |
| 197 | + |
| 198 | + t2 <- stat.test |
| 199 | + t3 <- merge(t2, t1, by = 0) |
| 200 | + stat.test <- t3[, 2:4] |
| 201 | + colnames(stat.test) <- c("group1", "group2", |
| 202 | + "p") |
| 203 | + stat.test <- stat.test %>% plyr::mutate(p.adj.signif = case_when(p > |
| 204 | + 0.05 ~ "NS", p <= 0.05 & p > 0.01 ~ "*", |
| 205 | + p <= 0.01 ~ "**")) |
| 206 | + stat.test <- stat.test[stat.test$p.adj.signif != |
| 207 | + "NS", ] |
| 208 | + if (length(stat.test>4)){ |
| 209 | + ggpubr::ggboxplot(data[["Data_renamed"]], |
| 210 | + x = "Group", |
| 211 | + y = colnames(data[["Data_renamed"]])[number], |
| 212 | + color = "Group", |
| 213 | + palette = ckey, |
| 214 | + add = "jitter", |
| 215 | + order = order, |
| 216 | + width = width)+ |
| 217 | + scale_y_continuous(label=ecoflux::scientific_10x)+ |
| 218 | + labs(title=NAMES[number],x =NULL, y = "Intensity")+ |
| 219 | + theme_classic()+ ggplot2::theme(plot.title=element_text(size=15, |
| 220 | + face="bold", |
| 221 | + color="Black", |
| 222 | + hjust=0.5, |
| 223 | + lineheight=1.2), # title |
| 224 | + plot.subtitle=element_text(size=15, |
| 225 | + hjust=0.5), # subtitle |
| 226 | + plot.caption=element_text(size=15), # caption |
| 227 | + axis.title.x=element_text(vjust=10, |
| 228 | + size=15), # X axis title |
| 229 | + axis.title.y=element_text(size=12), # Y axis title |
| 230 | + axis.text.x=element_text(size=10, |
| 231 | + vjust=.5), # X axis text |
| 232 | + axis.text.y=element_text(size=10), |
| 233 | + legend.position = legend_position, |
| 234 | + step.increase = step_increase, |
| 235 | + label.size = label_size, |
| 236 | + tip.length = tip_length)+ |
| 237 | + ggpubr::stat_pvalue_manual(stat.test, y.position = 1.05 * max(data[["Data_renamed"]][,number]), step.increase = 0.1,label = "p.adj.signif",size = 3.5,vjust = 0.05) |
| 238 | + ggsave(filename = paste(NAMES[number],"boxplot.png",collapse = ""), |
| 239 | + path=paste0(getwd(),"/boxplot"), |
| 240 | + width = fig_width, |
| 241 | + height = fig_height)} |
| 242 | + else if(significant_variable_only == F) { |
| 243 | + ggpubr::ggboxplot(data[["Data_renamed"]], |
| 244 | + x = "Group", |
| 245 | + y = colnames(data[["Data_renamed"]])[number], |
| 246 | + color = "Group", |
| 247 | + palette = ckey, |
| 248 | + add = "jitter", |
| 249 | + order = order, |
| 250 | + width = width)+ |
| 251 | + scale_y_continuous(label=ecoflux::scientific_10x)+ |
| 252 | + labs(title=NAMES[number],x =NULL, y = "Intensity")+ |
| 253 | + theme_classic()+ ggplot2::theme(plot.title=element_text(size=15, |
| 254 | + face="bold", |
| 255 | + color="Black", |
| 256 | + hjust=0.5, |
| 257 | + lineheight=1.2), # title |
| 258 | + plot.subtitle=element_text(size=15, |
| 259 | + hjust=0.5), # subtitle |
| 260 | + plot.caption=element_text(size=15), # caption |
| 261 | + axis.title.x=element_text(vjust=10, |
| 262 | + size=15), # X axis title |
| 263 | + axis.title.y=element_text(size=12), # Y axis title |
| 264 | + axis.text.x=element_text(size=10, |
| 265 | + vjust=.5), # X axis text |
| 266 | + axis.text.y=element_text(size=10), |
| 267 | + legend.position = legend_position) |
| 268 | + |
| 269 | + ggsave(filename = paste(NAMES[number],"boxplot.png",collapse = ""), |
| 270 | + path=paste0(getwd(),"/boxplot"), |
| 271 | + width = fig_width, |
| 272 | + height = fig_height)}}} |
| 273 | + |
| 274 | + ) |
| 275 | +} |
0 commit comments