1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| require(ggrepel) f_ggplot2_ti <- function(p, title, subtitle=''){ if (subtitle == ''){ (p + ggtitle(title) + theme(plot.title = element_text(hjust = 0.5))) }else{ (p + ggtitle(title, subtitle=subtitle) + theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))) } } f_DEmetabolism_Volcano <- function(countexpN, countexpE, metaN, typeN, delta = 1, Threshold_p = 0.05, lc_rep=5){ DEmm <- f_DEmetabolism(countexpN, countexpE, metaN, typeN) col_vector = rep(rgb(108, 200, 228, maxColorValue = 255), nrow(DEmm)) Threshold = with(DEmm, delta*sd(abs(def))) col_vector[with(DEmm, `p-value` < Threshold_p & def > Threshold)] = rgb(226, 61, 75, maxColorValue = 255) col_vector[with(DEmm, `p-value` < Threshold_p & def < -Threshold)] = rgb(232, 168, 71, maxColorValue = 255) DEmm[['Name']] <- rownames(DEmm) DEmm[['col']] <- col_vector DEmm[['-log10(P)']] <- with(DEmm, -log10(`p-value`)) lc_tp_logFC <- DEmm$def lc_tp_logFC[with(DEmm, `p-value` >= Threshold_p)] = 0 lc_idx <- order(lc_tp_logFC)[c(1:lc_rep, (nrow(DEmm)+1-lc_rep):nrow(DEmm))] res <- ggplot() + geom_point(aes(def, `-log10(P)`, col=I(col)), data = DEmm) res <- res + theme_bw() + theme(panel.grid=element_line(colour=NA)) res <- res + geom_hline(yintercept=-log10(Threshold_p), linetype="longdash") res <- res + geom_vline(xintercept=c(Threshold, -Threshold), linetype="longdash") res <- res + geom_text_repel(data=DEmm[lc_idx,],aes(def,`-log10(P)`,label=Name), force=T, max.overlaps=Inf) f_ggplot2_ti(res, typeN) }
|