Skip to content

Commit e185776

Browse files
committed
feat: added functions to customize colors in geom_peak
1 parent 8658399 commit e185776

File tree

7 files changed

+145
-90
lines changed

7 files changed

+145
-90
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ VignetteBuilder:
5757
knitr
5858
biocViews:
5959
Encoding: UTF-8
60-
RoxygenNote: 7.3.1
60+
RoxygenNote: 7.3.2

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ importFrom(ggplot2,ggplot_add)
114114
importFrom(ggplot2,labs)
115115
importFrom(ggplot2,margin)
116116
importFrom(ggplot2,rel)
117+
importFrom(ggplot2,scale_color_continuous)
117118
importFrom(ggplot2,scale_color_gradientn)
118119
importFrom(ggplot2,scale_color_manual)
119120
importFrom(ggplot2,scale_fill_manual)

R/geom_coverage.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
119119
if (length(color) < length(unique(data[, group.key]))) {
120120
warning("Fewer colors provided than there are groups in ", group.key, " variable, falling back to default colors")
121121
# sample group with same color
122-
fill.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
122+
fill.color <- AutoColor(data = data[[group.key]], pal = "Set1")
123123
} else {
124124
fill.color <- color
125125
}
@@ -140,7 +140,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
140140
if (length(color) != length(unique(data[, group.key]))) {
141141
warning("The color you provided is not as long as ", group.key, " column in data, select automatically!")
142142
# sample group with same color
143-
tmp.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
143+
tmp.color <- AutoColor(data = data[[group.key]], pal = "Set1")
144144
# change group key color
145145
color.color.df <- merge(unique(data[c(group.key)]), data.frame(color = tmp.color), by.x = group.key, by.y = 0)
146146
color.color <- color.color.df$color
@@ -169,7 +169,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
169169
fill.str.len <- length(unique(data[, fill.str]))
170170
if (is.null(color) | length(color) != fill.str.len) {
171171
# sample group with same color
172-
tmp.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
172+
tmp.color <- AutoColor(data = data[[group.key]], pal = "Set1")
173173
# change color
174174
fill.color.df <- merge(unique(data[c(fill.str, group.key)]), data.frame(color = tmp.color), by.x = group.key, by.y = 0)
175175
fill.color <- fill.color.df$color
@@ -191,7 +191,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
191191
color.str.len <- length(unique(data[, color.str]))
192192
if (is.null(color) | length(color) != color.str.len) {
193193
# sample group with same color
194-
tmp.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
194+
tmp.color <- AutoColor(data = data[[group.key]], pal = "Set1")
195195
# change color
196196
if (color.str == group.key) {
197197
color.color.df <- merge(unique(data[c(color.str)]), data.frame(color = tmp.color), by.x = group.key, by.y = 0)
@@ -223,7 +223,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
223223

224224
# facet color
225225
if (is.null(facet.color)) {
226-
facet.color <- AutoColor(data = data, n = 12, name = "Set3", key = facet.key)
226+
facet.color <- AutoColor(data = data[[facet.key]], pal = "Set3")
227227
}
228228

229229
# facet formula

R/geom_feature.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ ggplot_add.feature <- function(object, plot, object_name) {
9999
}
100100
} else {
101101
warning("The color you provided is smaller than Type column in data, select automatically!")
102-
used.feature.color <- AutoColor(data = valid.feature, n = 9, name = "Set1", key = "Type")
102+
used.feature.color <- AutoColor(data = valid.feature$Type, pal = "Set1")
103103
}
104104

105105
# create plot

R/geom_peak.R

Lines changed: 84 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,91 @@
22
#'
33
#' @param bed.file The path to consensus peaks file. Default: NULL.
44
#' @param peak.df The dataframe contains consensus peaks. Default: NULL.
5-
#' @param peak.color Peak color. Default: black.
5+
#' @param peak.color Peak colors. Default: NULL.
66
#' @param peak.size The line size of peak. Default: 5.
7+
#' @param color.by Name of optional column in bed file/data frame which is used for coloring. Default: NULL.
78
#' @param plot.space Top and bottom margin. Default: 0.1.
8-
#' @param plot.height The relative height of peak annotation to coverage plot. Default: 0.2.
9+
#' @param plot.height The relative height of peak annotation to coverage plot. Default: 0.1.
910
#'
1011
#' @return Plot.
1112
#' @importFrom utils read.table
1213
#' @importFrom dplyr arrange
1314
#' @importFrom dplyr %>%
14-
#' @importFrom ggplot2 ggplot_add ggplot geom_segment aes_string theme_classic theme element_blank element_text
15-
#' element_rect margin scale_x_continuous scale_y_continuous coord_cartesian
15+
#' @importFrom ggplot2 ggplot_add ggplot geom_segment aes_string theme_classic
16+
#' theme element_blank element_text element_rect margin scale_x_continuous
17+
#' scale_y_continuous scale_color_continuous coord_cartesian
1618
#' @export
1719
#'
1820
#' @examples
19-
#' # library(ggcoverage)
20-
#' # library(rtracklayer)
21-
#' # sample.meta <- data.frame(
22-
#' # SampleName = c("Chr18_MCF7_ER_1", "Chr18_MCF7_ER_2", "Chr18_MCF7_ER_3", "Chr18_MCF7_input"),
23-
#' # Type = c("MCF7_ER_1", "MCF7_ER_2", "MCF7_ER_3", "MCF7_input"),
24-
#' # Group = c("IP", "IP", "IP", "Input")
25-
#' # )
26-
#' # track folder
27-
#' # track.folder <- system.file("extdata", "ChIP-seq", package = "ggcoverage")
28-
#' # load bigwig file
29-
#' # track.df <- LoadTrackFile(
30-
#' # track.folder = track.folder, format = "bw",
31-
#' # meta.info = sample.meta
32-
#' # )
33-
#' # gtf.file <- system.file("extdata", "used_hg19.gtf", package = "ggcoverage")
34-
#' # gtf.gr <- rtracklayer::import.gff(con = gtf.file, format = "gtf")
35-
#' # create mark region
36-
#' # mark.region <- data.frame(start = c(76822533), end = c(76823743), label = c("Promoter"))
37-
#' # basic.coverage <- ggcoverage(
38-
#' # data = track.df, color = "auto", region = "chr18:76822285-76900000",
39-
#' # mark.region = mark.region, show.mark.label = FALSE
40-
#' # )
41-
#' # get consensus peak file
42-
#' # peak.file <- system.file("extdata", "ChIP-seq", "consensus.peak", package = "ggcoverage")
43-
#' # basic.coverage + geom_gene(gtf.gr = gtf.gr) + geom_peak(bed.file = peak.file)
44-
geom_peak <- function(bed.file = NULL, peak.df = NULL, peak.color = "black", peak.size = 5,
45-
plot.space = 0.1, plot.height = 0.1) {
21+
#' # load metadata
22+
#' sample_meta <- data.frame(
23+
#' SampleName = c(
24+
#' "Chr18_MCF7_ER_1",
25+
#' "Chr18_MCF7_ER_2",
26+
#' "Chr18_MCF7_ER_3",
27+
#' "Chr18_MCF7_input"
28+
#' ),
29+
#' Type = c("MCF7_ER_1", "MCF7_ER_2", "MCF7_ER_3", "MCF7_input"),
30+
#' Group = c("IP", "IP", "IP", "Input")
31+
#' )
32+
#'
33+
#' # import coverage track
34+
#' track_folder <- system.file("extdata", "ChIP-seq", package = "ggcoverage")
35+
#' track_df <- LoadTrackFile(
36+
#' track.folder = track_folder,
37+
#' format = "bw",
38+
#' region = "chr18:76822285-76900000",
39+
#' meta.info = sample_meta
40+
#' )
41+
#'
42+
#' # create mock peak file
43+
#' df_peaks <- data.frame(
44+
#' seqnames = c("chr18", "chr18", "chr18"),
45+
#' start = c(76822533, 76846900, 76880000),
46+
#' end = c(76836900, 76860000, 76887000),
47+
#' score = c(4, 6, 13)
48+
#' )
49+
#'
50+
#' # plot with default color
51+
#' ggcoverage(data = track_df) +
52+
#' geom_peak(peak.df = df_peaks, peak.size = 3)
53+
#'
54+
#' # plot with color by 'score' variable
55+
#' ggcoverage(data = track_df) +
56+
#' geom_peak(peak.df = df_peaks, peak.size = 3, color.by = "score")
57+
#'
58+
#' # plot with color by 'score' variable and custom color scale
59+
#' ggcoverage(data = track_df) +
60+
#' geom_peak(peak.df = df_peaks, peak.size = 3, color.by = "score", peak.color = rainbow(5))
61+
#'
62+
geom_peak <- function(bed.file = NULL, peak.df = NULL, peak.color = NULL, peak.size = 5,
63+
color.by = NULL, plot.space = 0.1, plot.height = 0.1) {
4664
structure(
4765
list(
4866
bed.file = bed.file, peak.df = peak.df, peak.color = peak.color, peak.size = peak.size,
49-
plot.space = plot.space, plot.height = plot.height
67+
color.by = color.by, plot.space = plot.space, plot.height = plot.height
5068
),
5169
class = "peak"
5270
)
5371
}
5472

5573
#' @export
5674
ggplot_add.peak <- function(object, plot, object_name) {
57-
# get plot data
58-
# plot.data <- plot$layers[[1]]$data
59-
# get plot data, plot data should contain bins
6075
if ("patchwork" %in% class(plot)) {
6176
plot.data <- plot[[1]]$layers[[1]]$data
6277
} else {
6378
plot.data <- plot$layers[[1]]$data
6479
}
65-
# prepare plot range
66-
# the plot region are not normal, so start is minimum value
6780
plot.chr <- as.character(plot.data[1, "seqnames"])
68-
# plot.region.start <- plot$coordinates$limits$x[1]
69-
# plot.region.end <- plot$coordinates$limits$x[2]
70-
# plot.region.start <- plot.data[1, "start"]
7181
plot.region.start <- min(plot.data[, "start"])
72-
# plot.region.end <- plot.data[nrow(plot.data), "end"]
7382
plot.region.end <- max(plot.data[, "end"])
7483

7584
# get parameters
7685
bed.file <- object$bed.file
7786
peak.df <- object$peak.df
7887
peak.color <- object$peak.color
7988
peak.size <- object$peak.size
89+
color.by <- object$color.by
8090
plot.space <- object$plot.space
8191
plot.height <- object$plot.height
8292

@@ -86,30 +96,55 @@ ggplot_add.peak <- function(object, plot, object_name) {
8696
} else if (!is.null(peak.df)) {
8797
bed.info <- peak.df
8898
}
89-
bed.info <- bed.info[c(1, 2, 3)]
90-
colnames(bed.info) <- c("seqnames", "start", "end")
91-
# convert to 1-based
99+
colnames(bed.info)[c(1, 2, 3)] <- c("seqnames", "start", "end")
92100
bed.info$start <- as.numeric(bed.info$start) + 1
93-
94-
# get valid bed
95101
valid.bed <- GetRegion(chr = plot.chr, df = bed.info, start = plot.region.start, end = plot.region.end)
96102

103+
# color management
104+
if (!is.null(color.by)) {
105+
if (is.numeric(valid.bed[[color.by]])) {
106+
if (!is.null(peak.color)) {
107+
scale_colors <- scale_color_gradientn(colours = peak.color)
108+
} else {
109+
scale_colors <- scale_color_continuous()
110+
}
111+
} else {
112+
if (length(peak.color) < length(unique(valid.bed[[color.by]]))) {
113+
warning("Fewer colors provided than there are groups in ", color.by, " variable, falling back to default colors")
114+
auto_colors <- AutoColor(data = valid.bed[[color.by]], pal = "Set3")
115+
scale_colors <- scale_color_manual(values = auto_colors)
116+
} else {
117+
scale_colors <- scale_color_manual(values = peak.color)
118+
}
119+
}
120+
} else {
121+
color.by <- "color"
122+
if (is.null(peak.color)) {
123+
peak.color <- "black"
124+
}
125+
valid.bed$color <- peak.color[1]
126+
scale_colors <- scale_color_manual(values = peak.color)
127+
}
128+
97129
peak.plot <- ggplot() +
98130
geom_segment(
99131
data = valid.bed,
100132
mapping = aes_string(
101133
x = "start",
102134
y = "1",
103135
xend = "end",
104-
yend = "1"
136+
yend = "1",
137+
color = color.by
105138
),
106139
size = peak.size,
107-
color = peak.color
108140
) +
141+
scale_colors +
109142
labs(y = "Peak")
110143

111144
# add theme
112-
peak.plot <- peak.plot + theme_peak(margin.len = plot.space, x.range = c(plot.region.start, plot.region.end))
145+
peak.plot <- peak.plot +
146+
theme_peak(margin.len = plot.space, x.range = c(plot.region.start, plot.region.end)) +
147+
theme(legend.position = "none")
113148
# assemble plot
114149
patchwork::wrap_plots(plot + theme(plot.margin = margin(t = plot.space, b = plot.space)),
115150
peak.plot,

R/utils.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ PrepareRegion <- function(region = NULL,
5555
}
5656

5757
# select color automatically
58-
AutoColor <- function(data, n, name, key) {
58+
AutoColor <- function(data, pal) {
5959
palettes <- list(
6060
Set1 = c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"),
6161
Set2 = c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3", "#A6D854", "#FFD92F", "#E5C494", "#B3B3B3"),
6262
Set3 = c("#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072", "#80B1D3", "#FDB462", "#B3DE69", "#FCCDE5", "#D9D9D9")
6363
)
64-
getPalette <- grDevices::colorRampPalette(palettes[[name]])
64+
get_palette <- grDevices::colorRampPalette(palettes[[pal]])
6565
# sample group with same color
66-
group.info <- unique(data[, key])
67-
fill.color <- getPalette(length(group.info))
68-
names(fill.color) <- group.info
69-
return(fill.color)
66+
data_levels <- unique(data)
67+
cols <- get_palette(length(data_levels))
68+
names(cols) <- data_levels
69+
return(cols)
7070
}
7171

7272
# create aa plot dataframe with padding offset

man/geom_peak.Rd

Lines changed: 47 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)