-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotRSD.R
112 lines (96 loc) · 3.61 KB
/
plotRSD.R
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
#' Plot RSD distributions
#' @rdname plotRSD
#' @description Plot RSD distributions of raw data in quality control samples.
#' @param analysis object of class `AnalysisData` or `Analysis`
#' @param cls information column to use for class labels
#' @param type `raw` or `pre-treated` data to plot
#' @param ... arguments to pass to the appropriate method
#' @importFrom stringr str_extract
#' @importFrom purrr map
#' @importFrom stats median
#' @importFrom ggplot2 geom_histogram geom_text geom_density
#' @importFrom patchwork plot_layout
#' @examples
#' library(metaboData)
#'
#' d <- analysisData(abr1$neg,abr1$fact)
#'
#' ## Plot class RSD distributions
#' plotRSD(d,cls = 'day')
#' @export
setGeneric('plotRSD',
function(analysis,
cls = 'class',
...)
standardGeneric('plotRSD'))
#' @rdname plotRSD
setMethod('plotRSD',signature = 'AnalysisData',
function(analysis, cls = 'class'){
if (clsExtract(analysis,cls) %>% is.numeric()) {
stop(
"Argument 'cls' should be either a factor or character",
call. = FALSE)
}
x <- rsd(analysis,cls = cls)
d <- ggplot(
x,
aes(
x = RSD,
colour = .data[[cls]],
group = .data[[cls]])) +
geom_density() +
theme_bw() +
labs(title = 'Density distrubution',
x = 'RSD (%)',
y = 'Density') +
theme(plot.title = element_text(face = 'bold',
hjust = 0.5),
axis.title = element_text(face = 'bold'),
legend.title = element_text(face = 'bold'),
legend.position = 'bottom',
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line = element_line())
cs <- x %>%
group_by_at(c(cls,'RSD')) %>%
summarise(sum = n()) %>%
mutate(cs = cumsum(sum))
csDist <- ggplot(
cs,
aes(
x = RSD,
y = cs,
colour = .data[[cls]])) +
geom_line() +
theme_bw() +
labs(title = 'Cumulative distribution',
x = 'RSD (%)',
y = 'Cumulative frequency') +
theme(plot.title = element_text(face = 'bold',
hjust = 0.5),
axis.title = element_text(face = 'bold'),
legend.title = element_text(face = 'bold'),
legend.position = 'bottom',
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line = element_line())
if (length(clsExtract(analysis,cls) %>% unique()) < 12) {
d <- d +
scale_colour_ptol()
csDist <- csDist +
scale_colour_ptol()
}
pl <- d + csDist
return(pl)
}
)
#' @rdname plotRSD
setMethod('plotRSD',signature = 'Analysis',
function(analysis,
cls = 'class',
type = 'raw'){
d <- analysisData(dat(analysis,type = type),
sinfo(analysis,type = type))
plotRSD(d,cls = cls)
}
)