-
Notifications
You must be signed in to change notification settings - Fork 6
/
plot-boxwhisker.R
168 lines (160 loc) · 5.53 KB
/
plot-boxwhisker.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
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
#' @title plotBoxWhisker
#' @description
#' Producing box-and-whisker plots
#'
#' @inheritParams addScatter
#' @param outliers Logical defining if outliers should be included in boxplot
#' @param dataMapping
#' A `BoxWhiskerDataMapping` object mapping `x`, `y` and aesthetic groups to their variable names of `data`.
#' @param plotConfiguration
#' An optional `BoxWhiskerConfiguration` object defining labels, grid, background and watermark.
#' @return A `ggplot` object
#'
#' @references For examples, see:
#' <https://www.open-systems-pharmacology.org/TLF-Library/articles/box-whisker-vignette.html>
#'
#' @export
#' @family molecule plots
#' @examples
#' # Produce box-and-whisker plots of log-normal distributed data
#' boxData <- data.frame(x = c(rep("A", 500), rep("B", 500)), y = rlnorm(1000))
#'
#' plotBoxWhisker(data = boxData, dataMapping = BoxWhiskerDataMapping$new(x = "x", y = "y"))
#'
#' # Remove outliers from boxplot
#' plotBoxWhisker(
#' data = boxData,
#' dataMapping = BoxWhiskerDataMapping$new(x = "x", y = "y"),
#' outliers = FALSE
#' )
#'
plotBoxWhisker <- function(data,
metaData = NULL,
outliers = NULL,
dataMapping = NULL,
plotConfiguration = NULL,
plotObject = NULL) {
#----- Validation and formatting of input arguments -----
validateIsNotEmpty(data)
validateIsOfType(data, "data.frame")
dataMapping <- .setDataMapping(dataMapping, BoxWhiskerDataMapping, data)
plotConfiguration <- .setPlotConfiguration(
plotConfiguration, BoxWhiskerPlotConfiguration,
data, metaData, dataMapping
)
# Overwrites plotConfiguration$outliers if outliers is not null
validateIsLogical(outliers, nullAllowed = TRUE)
plotConfiguration$outliers <- outliers
plotObject <- .setPlotObject(plotObject, plotConfiguration)
#----- Build layers of molecule plot -----
plotObject <- .addBoxWhisker(data, metaData, dataMapping, plotConfiguration, plotObject)
if (plotConfiguration$outliers) {
plotObject <- .addOutliers(data, metaData, dataMapping, plotConfiguration, plotObject)
}
plotObject <- .updateAxes(plotObject)
return(plotObject)
}
#' @title .addBoxWhisker
#' @description
#' Add a boxplot layer to a `ggplot` object (without outliers)
#'
#' @inheritParams plotBoxWhisker
#' @return A `ggplot` object
#' @keywords internal
.addBoxWhisker <- function(data, metaData, dataMapping, plotConfiguration, plotObject) {
# Get the box plot quantiles from dataMapping
mapData <- dataMapping$getBoxWhiskerLimits(data)
# Convert the mapping into characters usable by aes
mapLabels <- .getAesStringMapping(dataMapping)
aestheticValues <- .getAestheticValuesFromConfiguration(
n = 1,
position = 0,
plotConfigurationProperty = plotObject$plotConfiguration$ribbons,
propertyNames = c("size", "alpha", "linetype")
)
plotObject <- plotObject +
ggplot2::geom_boxplot(
data = mapData,
mapping = ggplot2::aes(
x = .data[[mapLabels$x]],
ymin = .data$ymin,
lower = .data$lower,
middle = .data$middle,
upper = .data$upper,
ymax = .data$ymax,
fill = .data[[mapLabels$fill]],
color = .data[[mapLabels$color]]
),
alpha = aestheticValues$alpha,
size = aestheticValues$size,
linetype = aestheticValues$linetype,
show.legend = TRUE,
stat = "identity"
)
plotObject <- .updateAesProperties(
plotObject,
plotConfigurationProperty = "ribbons",
propertyNames = c("color", "fill"),
data = mapData,
mapLabels = mapLabels
)
return(plotObject)
}
#' @title .addOutliers
#' @description
#' Add scatter points for outliers to `ggplot` object
#'
#' @inheritParams plotBoxWhisker
#' @return A `ggplot` object
#' @keywords internal
.addOutliers <- function(data, metaData, dataMapping, plotConfiguration, plotObject) {
mapData <- dataMapping$getOutliers(data)
# Convert the mapping into characters usable by aes
mapLabels <- .getAesStringMapping(dataMapping)
aestheticValues <- .getAestheticValuesFromConfiguration(
n = 1,
position = 0,
plotConfigurationProperty = plotObject$plotConfiguration$points,
propertyNames = c("size", "alpha", "shape", "color")
)
# addScatterLayer cannot be used in this case,
# because position dodge is needed to align boxes and outlier points
# no matter the number of groups, the value of 0.9 will be always fix
# otherwise, points won't be centered anymore
# besides, mapData includes NA instead of non-outlying data,
# na.rm removes these points without sending warning
plotObject <- plotObject +
geomTLFPoint(
data = mapData,
mapping = ggplot2::aes(
x = .data[[mapLabels$x]],
y = .data$maxOutliers,
group = .data[[mapLabels$fill]],
color = .data[[mapLabels$color]]
),
size = aestheticValues$size,
shape = aestheticValues$shape,
color = aestheticValues$color,
alpha = aestheticValues$alpha,
show.legend = TRUE,
na.rm = TRUE,
position = position_dodge(width = 0.9)
) +
geomTLFPoint(
data = mapData,
mapping = ggplot2::aes(
x = .data[[mapLabels$x]],
y = .data$minOutliers,
group = .data[[mapLabels$fill]],
color = .data[[mapLabels$color]]
),
size = aestheticValues$size,
shape = aestheticValues$shape,
color = aestheticValues$color,
alpha = aestheticValues$alpha,
show.legend = TRUE,
na.rm = TRUE,
position = position_dodge(width = 0.9)
)
return(plotObject)
}