comparison graph_stat_presence_abs.r @ 0:f9bce5117161 draft

"planemo upload for repository https://github.com/Marie59/Data_explo_tools commit 2f883743403105d9cac6d267496d985100da3958"
author ecology
date Tue, 27 Jul 2021 16:56:39 +0000
parents
children 4ed07d2d442b
comparison
equal deleted inserted replaced
-1:000000000000 0:f9bce5117161
1 #Rscript
2
3 ################################
4 ## Median and dispersion ##
5 ################################
6
7 #####Packages : Cowplot
8 # ggplot2
9
10 #####Load arguments
11
12 args <- commandArgs(trailingOnly = TRUE)
13
14 if (length(args) == 0) {
15 stop("This tool needs at least one argument")
16 }else{
17 table <- args[1]
18 hr <- args[2]
19 var <- as.numeric(args[3])
20 spe <- as.numeric(args[4])
21 loc <- as.numeric(args[5])
22 time <- as.numeric(args[6])
23 }
24
25 if (hr == "false") {
26 hr <- FALSE
27 }else{
28 hr <- TRUE
29 }
30
31 #####Import data
32 data <- read.table(table, sep = "\t", dec = ".", header = hr, fill = TRUE, encoding = "UTF-8")
33 data <- na.omit(data)
34 colvar <- colnames(data)[var]
35 colspe <- colnames(data)[spe]
36 colloc <- colnames(data)[loc]
37 coltime <- colnames(data)[time]
38
39 data <- data[grep("^$", data[, spe], invert = TRUE), ]
40 time <- as.integer(substring(data[, time], first = 1, last = 4))
41
42 #####Your analysis
43
44 ####Median and data dispersion####
45
46 #Median
47 graph_median <- function(data, var) {
48 graph_median <- ggplot2::ggplot(data, ggplot2::aes_string(y = var)) +
49 ggplot2::geom_boxplot(color = "darkblue") +
50 ggplot2::theme(legend.position = "none") + ggplot2::ggtitle("Median")
51
52 return(graph_median)
53
54 }
55
56 #Dispersion
57 dispersion <- function(data, var, var2) {
58 graph_dispersion <- ggplot2::ggplot(data) +
59 ggplot2::geom_point(ggplot2::aes_string(x = var2, y = var, color = var2)) +
60 ggplot2::scale_fill_brewer(palette = "Set3") +
61 ggplot2::theme(legend.position = "none", axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5, hjust = 1), plot.title = ggplot2::element_text(color = "black", size = 12, face = "bold")) + ggplot2::ggtitle("Dispersion")
62
63 return(graph_dispersion)
64
65 }
66
67 #The 2 graph
68 med_disp <- function(med, disp) {
69 graph <- cowplot::plot_grid(med, disp, ncol = 1, nrow = 2, vjust = -5, scales = "free")
70
71 ggplot2::ggsave("Med_Disp.png", graph, width = 12, height = 20, units = "cm")
72 }
73
74
75 #### Zero problem in data ####
76
77 #Put data in form
78 make_table_analyse <- function(data, var, spe, var2, var3) {
79 tab <- reshape(data
80 , v.names = var
81 , idvar = c(var2, var3)
82 , timevar = spe
83 , direction = "wide")
84 tab[is.na(tab)] <- 0 ###### remplace les na par des 0 / replace NAs by 0
85
86 colnames(tab) <- sub(var, "", colnames(tab))### remplace le premier pattern "abond." par le second "" / replace the column names "abond." by ""
87 return(tab)
88 }
89 data_num <- make_table_analyse(data, colvar, colspe, colloc, coltime)
90 nb_spe <- length(unique(data[, spe]))
91 nb_col <- ncol(data_num) - nb_spe + 1
92 data_num <- data_num[, nb_col:ncol(data_num)]
93
94 #Presence of zeros in the data
95 mat_corr <- function(data) {
96 cor(data)
97 }
98 p_mat <- function(data) {
99 ggcorrplot::cor_pmat(data)
100 } # compute a matrix of correlation p-values
101
102 graph_corr <- function(data_num) {
103 graph <- ggcorrplot::ggcorrplot(mat_corr(data_num), method = "circle", p.mat = p_mat(data_num), #barring the no significant coefficient
104 ggtheme = ggplot2::theme_gray, colors = c("#00AFBB", "#E7B800", "#FC4E07"))
105
106 ggplot2::ggsave("0_pb.png", graph)
107 }
108
109 ##Med and disp
110 med <- graph_median(data, var = colvar)
111 disp <- dispersion(data, var = colvar, var2 = colspe)
112 med_disp(med = med, disp = disp)
113
114 ##O problem
115 graph_corr(data_num)