TablesPlot.R 7.25 KB
Newer Older
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#@ This file contains all the functions for the
#@ plots in tab "Tables" of SHAMAN


###########################
##        Bar chart
###########################
Bar_Chart_Tables <- function(input, data, export = FALSE) {
  data_table <- data.table::data.table(data$complete)
  data_table <-
    data_table[order(eval(parse(text = input$ColumnOrder)), decreasing = input$Decreasing)]
  
  data_table <-
    data_table[data_table$Id %in% input$selectTaxoPlotBarChart,]
  
  #set colors
  x <- data_table[["log2FoldChange"]]
  y <- data_table[["pvalue_adjusted"]]
  y_log <- -1 * log10(y)
  y_signif <- -log10(as.numeric(input$AlphaVal))
  x_signif <-
    if (!input$setXsignifThreshold) {
      0
    } else{
      input$XsignifThreshold
    }
  x_up <- x > x_signif
  x_down <- x < -x_signif
  y_sig <- y_log > y_signif
  matrix <- cbind(x_up, x_down, y_sig)
  Group <- 
    apply(matrix, 1, function(point)
    if ((anyNA(point)) |
        (!point[[3]])) {
      "Not significant"
    } else{
      if (point[[1]]) {
        "Up"
      } else{
        if (point[[2]]) {
          "Down"
        } else{
          "Not significant"
        }
      }
    })
  colors <-
    apply(matrix, 1, function(point)
      if ((anyNA(point)) |
          (!point[[3]])) {
        input$colour2
      } else{
        if (point[[1]]) {
          input$colour3
        } else{
          if (point[[2]]) {
            input$colour1
          } else{
            input$colour2
          }
        }
      })
  data_table[["color"]] <- colors
  data_table[["Group"]] <- Group
  
  if(!export){
    plot <- amBarplot(
      x = "Id",
      y = "log2FoldChange",
      data = data_table,
      horiz = TRUE,
      ylab = "log 2 Fold Change",
      creditsPosition = "bottom-right"
    )
    plot <- setProperties(plot, fontSize = input$fontSize)
  }
  
78
  else{
79
80
    data <- data_table
    data$Id <- factor(data$Id, levels = rev(data$Id))
81
82
    plot <- ggplot(data, aes(x = Id, y = log2FoldChange)) + theme_minimal()
    plot <- plot + geom_bar(stat = "identity", aes(color = Group, fill = Group), width = 0.7)
83
84
85
86
87
88
89
90
    plot <- plot + coord_flip()
    plot <- plot + scale_color_manual(values = c("Down" = input$colour1,"Not significant" = input$colour2,"Up" = input$colour3))
    plot <- plot + scale_fill_manual(values = c("Down" = input$colour1,"Not significant" = input$colour2,"Up" = input$colour3))
    plot <- plot + labs(x = "", y = "log 2 Fold Change")
    plot <- plot + theme(axis.title = element_text(size = input$fontSize),
                         axis.text = element_text(size = input$fontSize),
                         legend.title = element_text(size = input$fontSize),
                         legend.text = element_text(size = input$fontSize))
91
92
    if(input$removeLabelsBarChart){plot <- plot + theme(axis.ticks.y = element_blank(),
                                                        axis.text.y = element_blank())}
93
  }
94
  return(plot)
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
}

###########################
##        Volcano plot
###########################
Volcano_Plot <- function(input, data, export = FALSE) {

  x <- data$complete[["log2FoldChange"]]
  y <- data$complete[["pvalue_adjusted"]]
  y_log <- -1 * log10(y)
  
  #set axes limits, horizontally centering the chart
  xmin <- -1 * max(-1 * min(x[!is.na(x)]), max(x[!is.na(x)]))
  xmax <- -xmin
  deltaX <- xmax - xmin
  ymin <- min(y_log[!is.na(y_log)])
  ymax <- max(y_log[!is.na(y_log)])
  deltaY <- ymax - ymin
  margin <- 0.1
  xlimits <- c(xmin - margin * deltaX, xmax + margin * deltaX)
  ylimits <- c(ymin - margin * deltaY / 10, ymax + margin * deltaY)
  
  #significance thresholds
  y_signif <- -log10(as.numeric(input$AlphaVal))
  x_signif <-
    if (!input$setXsignifThreshold) {
      0
    } else{
      input$XsignifThreshold
    }
  
  #comparison to thresholds
  x_up <- x > x_signif
  x_down <- x < -x_signif
  y_sig <- y_log > y_signif
  matrix <- cbind(x_up, x_down, y_sig)
  color_var <-
    apply(matrix, 1, function(point)
      if ((anyNA(point)) |
          (!point[[3]])) {
        "Not significant"
      } else{
        if (point[[1]]) {
          "Up"
        } else{
          if (point[[2]]) {
            "Down"
          } else{
            "Not significant"
          }
        }
      })
  
  names <- data$complete[["Id"]]
  points_to_label <- input$selectTaxoLabelVolcano
  labels <-
    sapply(names, function(name)
      if (is.element(name, points_to_label)) {
        points_to_label[match(name, points_to_label)]
      } else{
        ""
      })
  if(!export){
  plot <-
    scatterD3(
      x = x,
      y = y_log,
      xlab = "Log 2 Fold Change",
      ylab = "- Log 10 p value adjusted",
      hover_opacity = 1,
      tooltip_text = names,
      xlim = xlimits,
      ylim = ylimits,
      lines = if (input$showSignifThresholds) {
                  data.frame(
                            slope = c(Inf, Inf, 0),
                            intercept = c(-x_signif, x_signif, y_signif),
                            stroke = "black",
                            stroke_width = input$signifThresholdsWidth,
                            stroke_dasharray = 5
                            )
                  } 
              else{NULL},
      col_var = color_var,
179
      col_lab = NA, #if(input$showLegend){"Legend title"}else{NA}
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
      colors = c(
        "Down" = input$colour1,
        "Not significant" = input$colour2,
        "Up" = input$colour3
      ),
      lab = labels,
      point_opacity = input$pointOpacity,
      point_size = input$pointSize,
      axes_font_size = paste(input$axisFontSize, "0%", sep = ""),
      labels_size = input$labelsSize,
      # legend_font_size = paste(input$legendFontSize, "%", sep = ""), 
      # legend_width = input$legendWidth,
      dom_id_reset_zoom = "scatterD3-reset-zoomVolcano",
      dom_id_svg_export = "scatterD3-svg-exportVolcano",
      menu = FALSE,
      height = input$heightVolcanoPlot,
      width=if(input$modifwidthVolcano){input$widthVolcanoPlot},
197
      disable_wheel = FALSE
198
199
200
201
202
203
    )
  return(plot)
  }
  
  if(export){
    data <- data.frame(x,y_log, color_var)
204
    plot <- ggplot(data, aes(x = x, y = y_log)) + theme_minimal()
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    plot <- plot + geom_point(aes(color = color_var), size = (input$pointSize %/% 30), alpha = input$pointOpacity)
    plot <- plot + scale_x_continuous("Log 2 Fold Change", limits = xlimits)
    plot <- plot + scale_y_continuous("- Log 10 p value adjusted", limits = ylimits)
    plot <- plot + theme(axis.title = element_text(size = input$axisFontSize), 
                         axis.text = element_text(size = input$axisFontSize),
                         legend.title = element_text(size = input$axisFontSize),
                         legend.text = element_text(size = input$axisFontSize))
    plot <- plot + geom_text(aes(label = labels, color = color_var), size = input$labelsSize %/% 3, hjust = 1, vjust = 1, show.legend = FALSE)
    plot <- plot + scale_color_manual(name = "Group", values = c("Down" = input$colour1,"Not significant" = input$colour2,"Up" = input$colour3))
    if(input$showSignifThresholds){
      plot <- plot + geom_hline(yintercept = y_signif, linetype = "dashed", size = input$signifThresholdsWidth/2)
      plot <- plot + geom_vline(xintercept = x_signif, linetype = "dashed", size = input$signifThresholdsWidth/2) + geom_vline(xintercept = -x_signif, linetype = "dashed", size = input$signifThresholdsWidth/2)
    }
    return(plot)
  }
}