Skip to content

Commit d0c89d4

Browse files
committed
feat: geom_aggrcoverage
1 parent 48d7a97 commit d0c89d4

File tree

5 files changed

+148
-34
lines changed

5 files changed

+148
-34
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Imports:
3535
rtracklayer,
3636
methods,
3737
tidyr,
38+
ggplot2,
3839
dplyr,
3940
fansi,
4041
pillar,
@@ -46,7 +47,6 @@ Imports:
4647
Suggests:
4748
tidySummarizedExperiment,
4849
plyranges,
49-
ggplot2,
5050
TxDb.Hsapiens.UCSC.hg19.knownGene,
5151
AnnotationHub,
5252
GenomicFeatures,

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export(aggregate)
1111
export(as_tibble)
1212
export(coarsen)
1313
export(expand)
14+
export(geom_aggrcoverage)
1415
export(show)
1516
exportMethods(CoverageExperiment)
1617
exportMethods(aggregate)
@@ -19,6 +20,7 @@ exportMethods(show)
1920
import(GenomeInfoDb)
2021
import(GenomicRanges)
2122
import(SummarizedExperiment)
23+
import(ggplot2)
2224
import(methods)
2325
importFrom(BiocIO,resource)
2426
importFrom(BiocParallel,bplapply)

R/geoms.R

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#' geom_aggrcoverage
2+
#'
3+
#' #' @description
4+
#'
5+
#' `geom_aggrcoverage()`
6+
#'
7+
#' @name geom_aggrcoverage
8+
#' @rdname geom_aggrcoverage
9+
#'
10+
#' @return A `ggplot` object`
11+
#'
12+
#' @import ggplot2
13+
#'
14+
#' @examples
15+
#' library(rtracklayer)
16+
#' library(plyranges)
17+
#' library(ggplot2)
18+
#' TSSs_bed <- system.file("extdata", "TSSs.bed", package = "tidyCoverage")
19+
#' features <- list(
20+
#' TSS_fwd = import(TSSs_bed) |> filter(strand == '+'),
21+
#' TSS_rev = import(TSSs_bed) |> filter(strand == '-')
22+
#' )
23+
#' tracks <- list(
24+
#' RNA_fwd = system.file("extdata", "RNA.fwd.bw", package = "tidyCoverage"),
25+
#' RNA_rev = system.file("extdata", "RNA.rev.bw", package = "tidyCoverage")
26+
#' ) |> map(import, as = 'Rle')
27+
#' df <- CoverageExperiment(tracks, features, width = 5000, ignore.strand = FALSE) |>
28+
#' aggregate() |>
29+
#' as_tibble()
30+
NULL
31+
32+
GeomAggrCoverage <- ggplot2::ggproto("GeomAggrCoverage", ggplot2::Geom,
33+
setup_params = function(data, params) {
34+
params$ci <- params$ci
35+
params
36+
},
37+
extra_params = c("na.rm"),
38+
required_aes = c("x", "y", "ymin", "ymax"),
39+
default_aes = ggplot2::aes(
40+
colour = "black",
41+
linewidth = 1,
42+
linetype = 1,
43+
alpha = 0.4
44+
),
45+
draw_group = function(data, params, coord, ci = TRUE, ...) {
46+
forLine <- transform(data, alpha = 1)
47+
forRibbon <- transform(
48+
data,
49+
alpha = data$alpha,
50+
fill = data$colour,
51+
colour = NA
52+
)
53+
grid::gList(
54+
if (ci) ggplot2::GeomRibbon$draw_panel(forRibbon, params, coord, ...),
55+
ggplot2::GeomLine$draw_panel(forLine, params, coord, ...)
56+
)
57+
},
58+
draw_key = ggplot2::draw_key_smooth
59+
)
60+
61+
#' @rdname geom_aggrcoverage
62+
#' @export
63+
64+
geom_aggrcoverage <- function(
65+
mapping = NULL,
66+
data = NULL,
67+
...,
68+
ci = TRUE,
69+
na.rm = FALSE,
70+
show.legend = NA,
71+
inherit.aes = TRUE
72+
) {
73+
m <- ggplot2::aes(x = coord, y = mean, ymin = ci_low, ymax = ci_high, group = interaction(.sample, .feature))
74+
if (!is.null(mapping)) m <- utils::modifyList(m, mapping)
75+
76+
ggplot2::layer(
77+
data = data,
78+
mapping = m,
79+
stat = "identity",
80+
geom = GeomAggrCoverage,
81+
position = "identity",
82+
show.legend = show.legend,
83+
inherit.aes = inherit.aes,
84+
params = list(na.rm = na.rm, ci = ci, ...)
85+
)
86+
}
87+

man/geom_aggrcoverage.Rd

+42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/tidyCoverage.Rmd

+16-33
Original file line numberDiff line numberDiff line change
@@ -403,39 +403,27 @@ aggregated coverage signal of multiple tracks over multiple sets of genomic rang
403403
```{r}
404404
AC |>
405405
as_tibble() |>
406-
ggplot(aes(x = coord, y = mean, group = interaction(features, track), col = track)) +
407-
geom_line()
406+
ggplot() +
407+
geom_aggrcoverage()
408408
```
409409

410-
This plot is way too busy. Let's first split into facets using `features`:
410+
Oopsie, a little busy here. Let's color by tracks and split facets by `features`:
411411

412412
```{r}
413413
AC |>
414414
as_tibble() |>
415-
ggplot(aes(x = coord, y = mean, col = track)) +
416-
geom_line() +
415+
ggplot(aes(col = track)) +
416+
geom_aggrcoverage() +
417417
facet_grid(features ~ .)
418418
```
419419

420-
Better, but what about adding confidence interval for each coverage track:
420+
Nearly there, few cosmethic changes and we are done!
421421

422422
```{r}
423423
AC |>
424424
as_tibble() |>
425-
ggplot(aes(x = coord, y = mean)) +
426-
geom_ribbon(aes(ymin = ci_low, ymax = ci_high, fill = track), alpha = 0.2) +
427-
geom_line(aes(col = track)) +
428-
facet_grid(features ~ .)
429-
```
430-
431-
Nearly there, few cosmethic changes and we're done!
432-
433-
```{r}
434-
AC |>
435-
as_tibble() |>
436-
ggplot(aes(x = coord, y = mean)) +
437-
geom_ribbon(aes(ymin = ci_low, ymax = ci_high, fill = track), alpha = 0.2) +
438-
geom_line(aes(col = track)) +
425+
ggplot(aes(col = track, linetype = track %in% c('RNA_fwd', 'RNA_rev'))) +
426+
geom_aggrcoverage() +
439427
facet_grid(features ~ .) +
440428
labs(x = 'Distance from genomic feature', y = 'Mean coverage (± 95% conf. intervale)') +
441429
theme_bw() +
@@ -470,9 +458,8 @@ if the `tidySummarizedExperiment` package id loaded.
470458

471459
```{r}
472460
AC |>
473-
ggplot(aes(x = coord, y = mean)) +
474-
geom_line(aes(col = track)) +
475-
facet_grid(track ~ .) +
461+
ggplot() +
462+
geom_aggrcoverage() +
476463
labs(x = 'Distance from locus of convergent transcription', y = 'Scc1 coverage') +
477464
theme_bw() +
478465
theme(legend.position = 'top')
@@ -487,10 +474,8 @@ AC |>
487474
CoverageExperiment(tracks, features, width = 5000, scale = TRUE, center = TRUE) |>
488475
filter(track == 'RNA_fwd') |>
489476
aggregate(bin = 20) |>
490-
ggplot(aes(x = coord, y = mean)) +
491-
geom_ribbon(aes(ymin = ci_low, ymax = ci_high, fill = features), alpha = 0.2) +
492-
geom_line(aes(col = features)) +
493-
facet_grid(features ~ .) +
477+
ggplot(col = features) +
478+
geom_aggrcoverage(aes(col = features)) +
494479
labs(x = 'Distance to center of genomic features', y = 'Forward RNA-seq coverage') +
495480
theme_bw() +
496481
theme(legend.position = 'top')
@@ -538,9 +523,8 @@ CoverageExperiment(
538523
scale = TRUE, center = TRUE
539524
) |>
540525
aggregate() |>
541-
ggplot(aes(x = coord, y = mean)) +
542-
geom_ribbon(aes(ymin = ci_low, ymax = ci_high, fill = track), alpha = 0.2) +
543-
geom_line(aes(col = track)) +
526+
ggplot() +
527+
geom_aggrcoverage(aes(col = track)) +
544528
facet_grid(track ~ .) +
545529
labs(x = 'Distance from TSSs', y = 'Mean coverage') +
546530
theme_bw() +
@@ -586,9 +570,8 @@ AC |>
586570
stringr::str_detect(track, 'H3') ~ "H3"
587571
)
588572
) |>
589-
ggplot(aes(x = coord, y = mean)) +
590-
geom_ribbon(aes(ymin = ci_low, ymax = ci_high, fill = track), alpha = 0.2) +
591-
geom_line(aes(col = track)) +
573+
ggplot() +
574+
geom_aggrcoverage(aes(col = track)) +
592575
facet_grid(~histone) +
593576
labs(x = 'Distance from TSSs', y = 'Mean histone PTM coverage') +
594577
theme_bw() +

0 commit comments

Comments
 (0)