[d84c2d]: / R / plots.R

Download this file

268 lines (255 with data), 9.3 kB

  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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#' Plot p-values from moderated statistical tests for class biotmle
#'
#' Histogram of raw or FDR-adjusted p-values from the moderated t-test.
#'
#' @param x object of class \code{biotmle} as produced by an appropriate call
#' to \code{biomarkertmle}
#' @param type character describing whether to provide a plot of unadjusted or
#' adjusted p-values (adjustment performed via Benjamini-Hochberg)
#' @param ... additional arguments passed \code{plot} as necessary
#'
#' @importFrom ggplot2 ggplot aes geom_histogram guides guide_legend xlab ylab
#' ggtitle theme_bw
#'
#' @return object of class \code{ggplot} containing a histogram of the raw or
#' Benjamini-Hochberg corrected p-values (depending on user input).
#'
#' @export
#'
#' @method plot bioTMLE
#'
#' @examples
#' \dontrun{
#' library(dplyr)
#' library(biotmleData)
#' library(SuperLearner)
#' library(SummarizedExperiment)
#' data(illuminaData)
#'
#' colData(illuminaData) <- colData(illuminaData) %>%
#' data.frame() %>%
#' mutate(age = as.numeric(age > median(age))) %>%
#' DataFrame()
#' benz_idx <- which(names(colData(illuminaData)) %in% "benzene")
#'
#' biomarkerTMLEout <- biomarkertmle(
#' se = illuminaData,
#' varInt = benz_idx,
#' bppar_type = BiocParallel::SerialParam(),
#' g_lib = c("SL.mean", "SL.glm"),
#' Q_lib = c("SL.mean", "SL.glm")
#' )
#'
#' limmaTMLEout <- modtest_ic(biotmle = biomarkerTMLEout)
#'
#' plot(x = limmaTMLEout, type = "pvals_adj")
#' }
plot.bioTMLE <- function(x, ..., type = "pvals_adj") {
if (type == "pvals_raw") {
p <- ggplot2::ggplot(x@topTable, ggplot2::aes(P.Value)) +
ggplot2::geom_histogram(ggplot2::aes(
y = ..count..,
), colour = "white", na.rm = TRUE, binwidth = 0.025) +
ggplot2::ggtitle("Histogram of raw p-values") +
ggplot2::xlab("magnitude of raw p-values")
} else if (type == "pvals_adj") {
p <- ggplot2::ggplot(
as.data.frame(x@topTable),
ggplot2::aes(adj.P.Val)
) +
ggplot2::geom_histogram(ggplot2::aes(
y = ..count..,
), colour = "white", na.rm = TRUE, binwidth = 0.025) +
ggplot2::ggtitle("Histogram of BH-corrected FDR p-values") +
ggplot2::xlab("magnitude of BH-corrected p-values")
}
p <- p +
ggplot2::guides(fill = ggplot2::guide_legend(title = NULL)) +
ggplot2::theme_bw() +
ggplot2::theme(legend.position = NULL)
return(p)
}
################################################################################
#' Volcano plot for class biotmle
#'
#' Volcano plot of the log-changes in the target causal paramter against the
#' log raw p-values from the moderated t-test.
#'
#' @param biotmle object of class \code{biotmle} as produced by an appropriate
#' call to \code{biomarkertmle}
#' @param ate_bound A \code{numeric} indicating the highest magnitude of the
#' average treatment effect to be colored on the x-axis of the volcano plot;
#' this limits the observations to be considered differentially expressed to
#' those in a user-specified interval.
#' @param pval_bound A \code{numeric} indicating the largest corrected p-value
#' to be colored on the y-axis of the volcano plot; this limits observations
#' considered as differentially expressed to those in a user-specified
#' interval.
#'
#' @importFrom dplyr "%>%" arrange mutate select filter
#' @importFrom ggplot2 ggplot aes geom_point guides guide_legend xlab ylab
#' ggtitle theme_bw
#' @importFrom ggsci scale_fill_gsea
#' @importFrom stats quantile
#' @importFrom assertthat assert_that
#' @importFrom methods is
#'
#' @return object of class \code{ggplot} containing a standard volcano plot of
#' the log-fold change in the causal target parameter against the raw log
#' p-value computed from the moderated tests in \code{modtest_ic}.
#'
#' @export volcano_ic
#'
#' @examples
#' \dontrun{
#' library(dplyr)
#' library(biotmleData)
#' library(SuperLearner)
#' library(SummarizedExperiment)
#' data(illuminaData)
#'
#' colData(illuminaData) <- colData(illuminaData) %>%
#' data.frame() %>%
#' mutate(age = as.numeric(age > median(age))) %>%
#' DataFrame()
#' benz_idx <- which(names(colData(illuminaData)) %in% "benzene")
#'
#' biomarkerTMLEout <- biomarkertmle(
#' se = illuminaData,
#' varInt = benz_idx,
#' bppar_type = BiocParallel::SerialParam(),
#' g_lib = c("SL.mean", "SL.glm"),
#' Q_lib = c("SL.mean", "SL.glm")
#' )
#'
#' limmaTMLEout <- modtest_ic(biotmle = biomarkerTMLEout)
#'
#' volcano_ic(biotmle = limmaTMLEout)
#' }
volcano_ic <- function(biotmle, ate_bound = 1.0, pval_bound = 0.2) {
# check class since not a generic method
assertthat::assert_that(is(biotmle, "bioTMLE"))
tt_volcano <- biotmle@topTable %>%
dplyr::arrange(adj.P.Val) %>%
dplyr::mutate(
AveExpr = I(AveExpr),
logPval = -log10(P.Value),
color = ifelse((AveExpr > ate_bound) & (adj.P.Val < pval_bound), "1",
ifelse((AveExpr < -ate_bound) & (adj.P.Val < pval_bound),
"-1", "0"
)
)
) %>%
dplyr::select(which(colnames(.) %in% c("AveExpr", "logPval", "color"))) %>%
dplyr::filter((AveExpr > stats::quantile(AveExpr, probs = 0.05)) &
AveExpr < stats::quantile(AveExpr, probs = 0.95))
p <- ggplot2::ggplot(tt_volcano, ggplot2::aes(x = AveExpr, y = logPval)) +
ggplot2::geom_point(aes(colour = color)) +
ggplot2::xlab("Average Treatment Effect") +
ggplot2::ylab("-log10(raw p-value)") +
ggplot2::ggtitle("Volcano Plot: Average Treatment Effect") +
ggsci::scale_fill_gsea() +
ggplot2::guides(color = ggplot2::guide_legend(title = NULL)) +
ggplot2::theme_bw()
return(p)
}
################################################################################
utils::globalVariables(c(
"adj.P.Val", ".", "..count..", "P.Value", "color",
"AveExpr", "logPval"
))
#' Heatmap for class biotmle
#'
#' Heatmap of contributions of a select subset of biomarkers to the variable
#' importance measure changes as assessed by influence curve-based estimation,
#' across all subjects. The heatmap produced performs supervised clustering, as
#' per Pollard & van der Laan (2008) <doi:10.2202/1544-6115.1404>.
#'
#' @param x Object of class \code{biotmle} as produced by an appropriate call
#' to \code{biomarkertmle}.
#' @param design A vector giving the contrast to be displayed in the heatmap.
#' @param FDRcutoff Cutoff to be used in controlling the False Discovery Rate.
#' @param type A \code{character} describing whether to plot only a top number
#' (as defined by FDR-corrected p-value) of biomarkers or all biomarkers.
#' @param top Number of identified biomarkers to plot in the heatmap.
#' @param ... additional arguments passed to \code{superheat::superheat} as
#' necessary
#'
#' @importFrom dplyr "%>%" arrange filter slice
#' @importFrom superheat superheat
#' @importFrom assertthat assert_that
#' @importFrom methods is
#'
#' @return heatmap (from \pkg{superheat}) using hierarchical clustering to plot
#' the changes in the variable importance measure for all subjects across a
#' specified top number of biomarkers.
#'
#' @export heatmap_ic
#'
#' @examples
#' \dontrun{
#' library(dplyr)
#' library(biotmleData)
#' library(SummarizedExperiment)
#' data(illuminaData)
#'
#' colData(illuminaData) <- colData(illuminaData) %>%
#' data.frame() %>%
#' mutate(age = as.numeric(age > median(age))) %>%
#' DataFrame()
#' benz_idx <- which(names(colData(illuminaData)) %in% "benzene")
#'
#' biomarkerTMLEout <- biomarkertmle(
#' se = illuminaData,
#' varInt = benz_idx,
#' bppar_type = BiocParallel::SerialParam(),
#' g_lib = c("SL.mean", "SL.glm"),
#' Q_lib = c("SL.mean", "SL.glm")
#' )
#'
#' limmaTMLEout <- modtest_ic(biotmle = biomarkerTMLEout)
#'
#' heatmap_ic(x = limmaTMLEout, design = design, FDRcutoff = 0.05, top = 10)
#' }
heatmap_ic <- function(x, ..., design, FDRcutoff = 0.25,
type = c("top", "all"), top = 25) {
# check class since not a generic method
assertthat::assert_that(is(x, "bioTMLE"))
type <- match.arg(type)
if (type == "top") {
topbiomarkersFDR <- x@topTable %>%
dplyr::filter(adj.P.Val < FDRcutoff) %>%
dplyr::arrange(adj.P.Val) %>%
dplyr::slice(seq_len(top))
if (nrow(topbiomarkersFDR) < top) {
message(paste(top, "biomarkers not found below specified FDR cutoff."))
}
if (any(class(x@tmleOut) %in% "EList")) {
biomarkerTMLEout_top <- x@tmleOut$E %>%
data.frame() %>%
dplyr::filter(rownames(x) %in% topbiomarkersFDR$ID)
} else {
biomarkerTMLEout_top <- x@tmleOut %>%
dplyr::filter(rownames(x) %in% topbiomarkersFDR$ID)
}
plot_title <- paste("Supervised Heatmap of Top", top, "Biomarkers")
} else {
if (any(class(x@tmleOut) %in% "EList")) {
biomarkerTMLEout_top <- x@tmleOut$E %>%
as.data.frame()
} else {
biomarkerTMLEout_top <- x@tmleOut
}
plot_title <- "Heatmap of Biomarkers with Supervised Clustering"
}
# group labels
annot <- ifelse(design == 0, "Control", "Treated")
# build supervised heatmap
superheat::superheat(as.matrix(biomarkerTMLEout_top),
grid.hline.col = "white", force.grid.hline = TRUE,
grid.vline.col = "white", force.grid.vline = TRUE,
membership.cols = annot,
title = plot_title,
...
)
}