/
specificity-plots.R
574 lines (513 loc) · 23.6 KB
/
specificity-plots.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#' The function calcSummarySignal takes the input BED file(s)
#' in form of GRanges or GRangesList object, overlaps
#' it with all defined open chromatin regions across
#' conditions (e.g. cell types) and returns a matrix,
#' where each row is the input genomic region
#' (if overlap was found), each column is a condition,
#' and the value is a meam signal from regions where
#' overlap was found.
#
#' @param query Genomic regions to be analyzed. Can be GRanges or GRangesList
#' object.
#' @param signalMatrix Matrix with signal values in predfined regions, where
#' rows are predefined genomic regions, columns are conditions
#' (e.g. cell types in which the signal was measured).
#' First column contains information about the genomic region in
#' following form: chr_start_end.
#' Can be either data.frame or data.table object.
#' @return A list with named components:
#' signalSummaryMatrix - data.table with cell specific open chromatin signal
#' values for query regions
#' matrixStats - data.frame containing boxplot stats for individual
#' cell type
#'
#' @export
#' @examples
#' signalSummaryList = calcSummarySignal(vistaEnhancers, exampleOpenSignalMatrix_hg19)
calcSummarySignal = function(query, signalMatrix){
.validateInputs(list(query=c("GRanges","GRangesList")))
if (is(query, "GRangesList")) {
# Recurse over each GRanges object
regionSummaryList = lapply(query, calcSummarySignal, signalMatrix)
nameList = names(query)
if(is.null(nameList)) {
nameList = seq_along(query) # Fallback to sequential numbers
}
# Extract signal matrices and boxplot matrices
# rbind them and add a name column
signalList = lapply(regionSummaryList, `[[`, 1)
statsList = lapply(regionSummaryList, `[[`, 2)
signalSummaryMatrix = rbindlist(signalList)
matrixStats = rbindlist(statsList)
signalSummaryMatrix$name = rep(nameList, vapply(signalList, nrow, integer(1)))
matrixStats$name = rep(nameList, vapply(statsList, nrow, integer(1)))
statNames = c("lowerWhisker", "lowerHinge", "median",
"upperHinge", "upperWhisker")
matrixStats$boxStats = rep(statNames, length(statsList))
regionSummaries = list(signalSummaryMatrix = signalSummaryMatrix, matrixStats = matrixStats)
return(regionSummaries)
}
# if the signalMatrix is in data.frame format, convert it to data.table
if(!is(signalMatrix, "data.table") && is(signalMatrix, "data.frame")){
signalMatrix = as.data.table(signalMatrix)
} else if (!is(signalMatrix, "data.table")){
stop("The signalMatrix object is in incorrect format - must be data.table or
data.frame.")
}
# get the genomic coordinates from the open chromatin signal matrix
openRegions = signalMatrixToOpenRegions(signalMatrix)
# if unknown chromosomes were tossed, update signalMatrix
if (nrow(openRegions) != nrow(signalMatrix)){
keepRegions = openRegions[, peakName:=paste(chr, start, end, sep="_")]
keepRegions = keepRegions[, peakName]
signalMatrix = signalMatrix[V1 %in% keepRegions, ]
}
# convert GRanges query to data.table
queryTable = queryToDataTable(query)
# find overlaps between query and signalMatrix regions
signalSummaryMatrix = getSignalMatrix(queryTable, openRegions, signalMatrix)
# get box plot statistics about the signal matrix
matrixStatsTable = getMatrixStats(signalSummaryMatrix)
# create list containing matrix with signal values, and matrix with boxplot stats
signalSummaryList = list(signalSummaryMatrix = signalSummaryMatrix,
matrixStats = matrixStatsTable)
return(signalSummaryList)
}
#' The function plotSummarySignal visualizes the signalSummaryMatrix obtained from
#' \code{calcSummarySignal}.
#'
#' @param signalSummaryList Output list from \code{calcSummarySignal} function.
#' @param plotType Options are: "jitter" - jitter plot with box plot on top,
#' "boxPlot" - box plot without individual points and outliers,
#' "barPlot" (default) - bar height represents the median signal value
#' for a given cell type,
#' "violinPlot" - violin plot with medians.
#' @param metadata (optional) data.table used for grouping columns from
#' 'signalMatrix' into categories, that are then plotted with different colors.
#' Must contain variable 'colName' that contains all the condition column names
#' from 'signaMatrix'.
#' @param colorColumn (optional only if metadata provided) columns name from
#' 'metadata' table that will be used as grouping variable for coloring.
#' @param filterGroupColumn (optional only if metadata provided and
#' 'filterGroup' specified) allows user to plot specified subgroups only.
#' String specifying the column name in 'metadata' from which groups will
#' be filtered (groups are specified in as 'filterGroups)
#' @param filterGroup (optional only if 'metadata' and 'filterGroupColumn'
#' provided) - string (or vector of strings) of groups from
#' 'filterGroupColumn' to be plottted.
#' @return A ggplot object.
#'
#' @export
#' @examples
#' signalSummaryList = calcSummarySignal(vistaEnhancers, exampleOpenSignalMatrix_hg19)
#' metadata = cellTypeMetadata
#' plotSignal = plotSummarySignal(signalSummaryList)
#'
#' plotSignalTissueColor = plotSummarySignal(signalSummaryList = signalSummaryList,
#' plotType = "jitter", metadata = metadata, colorColumn = "tissueType")
#'
#' plotSignalFiltered = plotSummarySignal(signalSummaryList = signalSummaryList,
#' plotType = "violinPlot", metadata = metadata, colorColumn = "tissueType",
#' filterGroupColumn = "tissueType", filterGroup = c("skin", "blood"))
plotSummarySignal = function(signalSummaryList,
plotType="barPlot",
metadata=NULL,
colorColumn=NULL,
filterGroupColumn=NULL,
filterGroup=NULL){
if(is.null(metadata) & (any(c(!is.null(colorColumn),
!is.null(filterGroupColumn),
!is.null(filterGroup))))){
stop("In order to color or filter groups, you must provide metadata.")
}
# reshape signal matrix and stats into matrix usable by ggplot
# and attach cell type metadata
plotTables = reshapeDataToPlot(signalSummaryList, metadata, colorColumn)
# if user defines cell group, filter the data
filteredPlotTables = filterGroups(plotTables, metadata,
filterGroupColumn, filterGroup)
plotSignalMatrix = filteredPlotTables$plotSignalMatrix
plotBoxStats = filteredPlotTables$plotBoxStats
# do the plotting
if (plotType == "jitter"){
jitterPlot = SummarySignalJitterPlot(plotSignalMatrix, colorColumn)
return(jitterPlot)
} else if (plotType == "boxPlot") {
boxPlot = SummarySignalBoxPlot(plotSignalMatrix, plotBoxStats, colorColumn)
return(boxPlot)
} else if (plotType == "barPlot") {
barPlot = SummarySignalBarPlot(plotBoxStats, colorColumn)
return(barPlot)
} else if (plotType == "violinPlot"){
violinPlot = SummarySignalViolinPlot(plotSignalMatrix,plotBoxStats, colorColumn)
return(violinPlot)
} else {
stop("Plot type does not match any of the available options.
Available options: jitter, boxPlot, barPlot, violinPlot. ")
}
}
# Internal helper function to extract genomic regions
# from matrix with open chromatin signal values
#
# @param signalMatrix Matrix with signal values in predfined regions, where
# rows are predefined genomic regions, columns are conditions
# (e.g. cell types in which the signal was measured).
# First column contains information about the genomic region in
# following form: chr_start_end.
# Can be either data.frame or data.table object.
# @return A data table with genomic coordinates of regions defined
# in 'signalMatrix'
signalMatrixToOpenRegions = function(signalMatrix){
# get the genomic coordinates from the open chromatin signal matrix -
# the first column convert the chr_start_end into a three column data.table
# unknown chromosomes are tossed
openRegions = signalMatrix[,1]
colnames(openRegions) = "V1"
#openRegions[, c("chr", "start", "end") := tstrsplit(V1, "_", fixed=TRUE)]
openRegions = setDT(tstrsplit(openRegions$V1, '[:_]', type.convert=TRUE))
if(ncol(openRegions) > 3){
openRegions = openRegions[is.na(V4),]
openRegions = openRegions[,c(1,2,3)]
}
setnames(openRegions, c("chr", "start", "end"))
numericColumns = c("start", "end")
openRegions[, (numericColumns ) := lapply(.SD, as.numeric),
.SDcols = numericColumns]
openRegions = openRegions[,.(chr, start, end)]
return(openRegions)
}
# Internal helper function to convert GRanges query into
# data.table and adds 4th column in form chr_start_end
#
# @param query Genomic regions to be analyzed in form of GRanges object.
# @return A data table with genomic coordinates from 'query' and
# 4th column containing peak identifiers
queryToDataTable = function(query){
# convert GRanges query to data.table
queryTable = as.data.table(data.frame(chr = seqnames(query),
start = start(query),
end = end(query)))
# select just the fist three columns and give them name chr, start, end
# create a 4th column with peak name in following format: chr_start_end
queryTable = queryTable[, c(1, 2, 3)]
colnames(queryTable) = c("chr", "start", "end")
queryTable[, peakName := paste(queryTable[,chr],
queryTable[,start],
queryTable[,end],
sep = "_")]
return(queryTable)
}
# Internal helper function to overlap two data.table objects and
# and summarize signal values from signalMatrix which fall into
# queryTable regions
#
# @param queryTable data.table created by \code{queryToDataTable}
# function
# @param openRegions data.table created by \code{signalMatrixToOpenRegions}
# function
# @param signalMatrix Matrix with signal values in predfined regions, where
# rows are predefined genomic regions, columns are conditions
# (e.g. cell types in which the signal was measured).
# First column contains information about the genomic region in
# following form: chr_start_end.
# Can be either data.frame or data.table object.
# @return A data.table with signal values for
getSignalMatrix = function(queryTable, openRegions, signalMatrix){
# find overlaps between query and signalMatrix regions
setkey(queryTable, chr, start, end)
overlaps = foverlaps(openRegions, queryTable, which=TRUE)
overlaps = na.omit(overlaps)
# extract the regions which overlap with query, assign the query peaks to
# them and calculate the sum of the signal within these regions
signalSummaryMatrix = signalMatrix[overlaps[,xid]]
signalSummaryMatrix = signalSummaryMatrix[,-1]
signalSummaryMatrix[, queryPeak := queryTable[overlaps[,yid], peakName]]
signalSummaryMatrix = signalSummaryMatrix[, lapply(.SD, max), by = .(queryPeak)]
return(signalSummaryMatrix)
}
# Internal helper function to obtain summary statistics about each
# column in the matrix
#
# @param signalSummaryMatrix Data.table object conatianing signal values
# for genomic regions (rows) across cell types (columns). The first
# column carries information about the genomic regions in form
# chr_start_end
# @return A data frame with boxplot statistics
getMatrixStats = function(signalSummaryMatrix){
# get box plot statistics about the signal matrix
matrixStatsList = apply(signalSummaryMatrix[,-1], 2, boxplot.stats)
matrixStats = unlist(lapply(matrixStatsList, `[[`, 1))
matrixStatsTable = data.frame(matrix(matrixStats, nrow = 5))
rownames(matrixStatsTable) = c("lowerWhisker", "lowerHinge", "median", "upperHinge", "upperWhisker")
colnames(matrixStatsTable) = names(matrixStatsList)
return(matrixStatsTable)
}
# Internal helper function to reshape signal matrix and stats matrix
# into a form, which can be plotted by ggplot
#
# @param signalSummaryList Results from \code{calcSummarySignal}.
# @param metadata Metadata as provided to \code{plotSummarySignal}
# @param colorColumn Column name specifying varibale for coloring
# as provided to \{plotSummarySignal}
#
# @return A list with two matrices reshaped for ggplot functions
reshapeDataToPlot = function(signalSummaryList, metadata, colorColumn){
# reshape the signal matrix ans boxplotStats matrices into ggplot usable form
# attach the metadata for coloring sort table alphabetically by
# tissueType-cellType
signalSummaryMatrix = signalSummaryList[["signalSummaryMatrix"]]
boxStats = signalSummaryList[["matrixStats"]]
if ("name" %in% names(signalSummaryMatrix)){
plotSignalMatrix = reshape2::melt(signalSummaryMatrix,
id.vars=c("queryPeak", "name"),
variable.name="colName", value.name="signal")
plotBoxStats = reshape2::melt(boxStats,
id.vars=c("boxStats", "name"),
variable.name="colName", value.name="value")
} else {
plotSignalMatrix = reshape2::melt(signalSummaryMatrix, id.vars="queryPeak",
variable.name="colName", value.name="signal")
boxStats$boxStats = rownames(boxStats)
plotBoxStats = reshape2::melt(boxStats, id.vars="boxStats",
variable.name="colName", value.name="value")
}
if(!is.null(metadata)){
data.table::setkey(metadata, colName)
data.table::setDT(plotSignalMatrix)
data.table::setkey(plotSignalMatrix, colName)
# attach metadata table to the plotSignalMatrix
plotSignalMatrix = plotSignalMatrix[metadata, on = "colName", nomatch=0]
data.table::setDT(plotBoxStats)
data.table::setkey(plotBoxStats, colName)
plotBoxStats = plotBoxStats[metadata, on = "colName", nomatch=0]
}
if(!is.null(colorColumn)){
plotSignalMatrix[, lowerColorColumn := tolower(get(colorColumn))]
data.table::setorder(plotSignalMatrix, lowerColorColumn, colName)
plotSignalMatrix[, mixedVar := paste(plotSignalMatrix[,get(colorColumn)],
plotSignalMatrix[,colName], sep="_")]
plotBoxStats[, lowerColorColumn := tolower(get(colorColumn))]
data.table::setorder(plotBoxStats, lowerColorColumn, colName)
plotBoxStats[, mixedVar := paste(plotBoxStats[,get(colorColumn)],
plotBoxStats[,colName], sep ="_")]
}
plotTables = list(plotSignalMatrix = plotSignalMatrix, plotBoxStats = plotBoxStats)
return(plotTables)
}
# Internal helper function to filter out tissue type predefined
# by user
# @param plotTables Result from \code{reshapeDataToPlot} function
# @param metadata Metadata as provided to \code{plotSummarySignal}
# @param filterGroupColumn Input to \code{plotSummarySignal} specifying
# from which 'metadata' column will be the filtering done.
# @param filterGroup string (or vector of strings) of groups from
# 'filterGroupColumn' to be plottted
#
# @return A list with two matrices filtered for user defined tissue type
filterGroups = function(plotTables,
metadata,
filterGroupColumn,
filterGroup){
plotSignalMatrix = plotTables$plotSignalMatrix
plotBoxStats = plotTables$plotBoxStats
if(length(filterGroupColumn) >1){
stop("Only one column from metadata can be specified
for filtering.")
} else if (is.null(filterGroupColumn) & !is.null(filterGroup)){
stop("You must provide the column name from
which groups are selected.")
} else if (!is.null(filterGroupColumn) & is.null(filterGroup)){
stop(paste0("Please specify which groups from ", filterGroupColumn,
"do you want plotted."))
} else if (is.null(filterGroupColumn) & is.null(filterGroup)){
plotSignalMatrix = plotSignalMatrix
plotBoxStats = plotBoxStats
} else {
if (length(filterGroup) == 1){
if (filterGroup %in% levels(factor(metadata[,get(filterGroupColumn)]))){
plotSignalMatrix = plotSignalMatrix[get(filterGroupColumn) == filterGroup]
plotBoxStats = plotBoxStats[get(filterGroupColumn) == filterGroup]
} else {
stop("The input cell group is not in predefined list of options.")
}
} else if (all(filterGroup %in% levels(factor(metadata[,get(filterGroupColumn)])))) {
plotSignalMatrix = plotSignalMatrix[get(filterGroupColumn) %in% filterGroup]
plotBoxStats = plotBoxStats[get(filterGroupColumn) %in% filterGroup]
} else {
stop("At least one of the input cell groups is not in predefined list of
options.")
}
}
plotTables = list(plotSignalMatrix = plotSignalMatrix, plotBoxStats = plotBoxStats)
return(plotTables)
}
# Internal helper function to plot jitter plot
#
# @param plotSignalMatrix Result from \code{reshapeDataToPlot} function followed
# by \code{filterGroups} function
# @param myLabels Labels created to group corresponding tissue type together
# @param colorColumn Column in 'plotSignalMatrix' used for coloring groups
#
# @return ggplot object - jitter plot
SummarySignalJitterPlot = function(plotSignalMatrix, colorColumn){
if("mixedVar" %in% names(plotSignalMatrix)){
myLabels = unique(plotSignalMatrix[, .(mixedVar, colName)])
myLabels[, spaceLabel := gsub("_", " ", myLabels[, colName])]
if(!is.null(colorColumn)){
jitterPlot = ggplot(plotSignalMatrix, aes(x=mixedVar, y=signal,
color=get(colorColumn)))+
labs(color = colorColumn)
} else{
jitterPlot = ggplot(plotSignalMatrix, aes(x=mixedVar, y=signal))
}
jitterPlot = jitterPlot + scale_x_discrete(labels=myLabels$spaceLabel)
} else {
jitterPlot = ggplot(plotSignalMatrix, aes(x=colName, y=signal))
}
if ("name" %in% names(plotSignalMatrix)){
jitterPlot = jitterPlot + facet_grid(name ~ .)
}
jitterPlot = jitterPlot +
geom_jitter(alpha=0.5, height=0, width=0.35) +
geom_boxplot(outlier.colour=NA, fill=NA) +
theme_bw() +
theme(axis.text.x = element_text(angle=90, hjust=1),
text = element_text(size=10)) +
xlab("") +
ylab("signal") +
theme_blank_facet_label() +
theme(strip.text.y.right = element_text(angle = 0))
return(jitterPlot)
}
# Internal helper function to plot box plot
#
# @param plotSignalMatrix Result from \code{reshapeDataToPlot} function followed
# by \code{filterGroups} function
# @param plotBoxStats Result from \code{reshapeDataToPlot} function followed
# by \code{filterGroups} function
# @param colorColumn Column in 'plotSignalMatrix' used for coloring groups
#
# @return ggplot object - box plot
SummarySignalBoxPlot = function(plotSignalMatrix, plotBoxStats,
colorColumn){
if("mixedVar" %in% names(plotSignalMatrix)){
myLabels = unique(plotSignalMatrix[, .(mixedVar, colName)])
myLabels[, spaceLabel := gsub("_", " ", myLabels[, colName])]
if(!is.null(colorColumn)){
boxPlot = ggplot(plotSignalMatrix, aes(x=mixedVar, y=signal,
fill=get(colorColumn)))+
labs(fill = colorColumn)
} else{
boxPlot = ggplot(plotSignalMatrix, aes(x = mixedVar, y = signal))
}
boxPlot = boxPlot + scale_x_discrete(labels=myLabels$spaceLabel)
} else {
boxPlot = ggplot(plotSignalMatrix, aes(x=colName, y=signal))
}
if ("name" %in% names(plotSignalMatrix)){
boxPlot = boxPlot + facet_grid(name ~ .)
}
boxPlot = boxPlot + geom_boxplot(outlier.colour=NA, alpha=0.9) +
theme_bw() +
theme(axis.text.x = element_text(angle=90, hjust=1),
text = element_text(size=10)) +
xlab("") +
ylab("normalized signal") +
ylim(min(plotBoxStats$value), max(plotBoxStats$value)) +
theme_blank_facet_label() +
theme(strip.text.y.right = element_text(angle = 0))
return(boxPlot)
}
# Internal helper function to plot bar plot
#
# @param plotBoxStats Result from \code{reshapeDataToPlot} function followed
# by \code{filterGroups} function
# @param myLabels Labels created to group corresponding tissue type together
# @param colorColumn Column in 'plotBoxStats' used for coloring groups
#
# @return ggplot object - bar plot
SummarySignalBarPlot = function(plotBoxStats,colorColumn){
if("mixedVar" %in% names(plotBoxStats)){
myLabels = unique(plotBoxStats[, .(mixedVar, colName)])
myLabels[, spaceLabel := gsub("_", " ", myLabels[, colName])]
if(!is.null(colorColumn)){
barPlot = ggplot(plotBoxStats[plotBoxStats$boxStats == "median",],
aes(x=mixedVar,
y=value,
fill=get(colorColumn))) +
labs(fill=colorColumn)
} else{
barPlot = ggplot(plotBoxStats[plotBoxStats$boxStats == "median",],
aes(x=mixedVar,
y=value))
}
barPlot = barPlot + scale_x_discrete(labels=myLabels$spaceLabel)
} else {
barPlot = ggplot(plotBoxStats[plotBoxStats$boxStats == "median",],
aes(x=colName,
y=value))
}
if ("name" %in% names(plotBoxStats)){
barPlot = barPlot + facet_grid(name ~ .)
}
barPlot = barPlot +
geom_col(alpha=0.9)+
theme_bw() +
theme(axis.text.x = element_text(angle=90, hjust=1),
text = element_text(size=10)) +
xlab("") +
ylab("med(signal)") +
theme_blank_facet_label() +
theme(strip.text.y.right = element_text(angle=0))
return(barPlot)
}
# Internal helper function to plot bar plot
#
# @param plotSignalMatrix Result from \code{reshapeDataToPlot} function followed
# by \code{filterGroups} function
# @param plotBoxStats Result from \code{reshapeDataToPlot} function followed
# by \code{filterGroups} function
# @param colorColumn Column in 'plotSignalMatrix' used for coloring groups
#
# @return ggplot object - violin plot
SummarySignalViolinPlot = function(plotSignalMatrix,plotBoxStats,
colorColumn){
if("mixedVar" %in% names(plotSignalMatrix)){
myLabels = unique(plotSignalMatrix[, .(mixedVar, colName)])
myLabels[, spaceLabel := gsub("_", " ", myLabels[, colName])]
if(!is.null(colorColumn)){
violinPlot = ggplot(plotSignalMatrix, aes(x=mixedVar, y=signal,
fill=get(colorColumn))) +
labs(fill = colorColumn)
} else{
violinPlot = ggplot(plotSignalMatrix, aes(x=mixedVar, y=signal))
}
violinPlot = violinPlot + scale_x_discrete(labels=myLabels$spaceLabel)
} else {
violinPlot = ggplot(plotSignalMatrix, aes(x=colName, y=signal))
}
if ("name" %in% names(plotSignalMatrix)){
violinPlot = violinPlot + facet_grid(name ~ .)
}
violinPlot = violinPlot +
geom_violin(alpha=0.8, scale = "width", trim = TRUE) +
theme_bw() +
theme(axis.text.x = element_text(angle=90, hjust=1),
text = element_text(size=10)) +
xlab("") +
ylab("signal") +
theme_blank_facet_label() +
theme(strip.text.y.right = element_text(angle = 0))
if("mixedVar" %in% names(plotSignalMatrix)){
violinPlot = violinPlot +
geom_point(data = plotBoxStats[plotBoxStats$boxStats == "median",],
aes(x = mixedVar, y = value),
color = "black", size = 2)
} else {
violinPlot = violinPlot +
geom_point(data = plotBoxStats[plotBoxStats$boxStats == "median",],
aes(x = colName, y = value),
color = "black", size = 2)
}
return(violinPlot)
}