3
3
# ' @param data Track prepared by \code{\link{FormatTrack}}.
4
4
# ' @param mapping Set of aesthetic mappings created by \code{aes} or \code{aes_}. Default: NULL.
5
5
# ' @param color Track color. Default: NULL (select automatically).
6
- # ' @param rect.color The color of every bin. Default: NA.
6
+ # ' @param rect.color The color of every bin. Default: NA
7
7
# ' @param single.nuc Logical value, whether to visualize at single nucleotide level (use bar plot). Default: FALSE.
8
8
# ' @param plot.type The type of the plot, choose from facet (separate plot for every sample) and
9
9
# ' joint (combine all sample in a single plot). Default: facet.
37
37
# '
38
38
# ' @export
39
39
# ' @examples
40
- # ' # library(ggcoverage)
41
- # ' # library(utils)
42
- # ' # library(ggplot2)
43
- # ' # meta.file <- system.file("extdata", "RNA-seq", "meta_info.csv", package = "ggcoverage")
44
- # ' # sample.meta <- utils::read.csv(meta.file)
45
- # ' # track folder
46
- # ' # track.folder <- system.file("extdata", "RNA-seq", package = "ggcoverage")
47
- # ' # load bigwig file
48
- # ' # track.df <- LoadTrackFile(
49
- # ' # track.folder = track.folder, format = "bw",
50
- # ' # meta.info = sample.meta
51
- # ' # )
52
- # ' # ggplot() +
53
- # ' # geom_coverage(data = track.df, color = "auto", mark.region = NULL)
40
+ # ' library(ggcoverage)
41
+ # ' library(ggplot2)
42
+ # '
43
+ # ' # import track data
44
+ # ' meta.file <- system.file("extdata", "RNA-seq", "meta_info.csv", package = "ggcoverage")
45
+ # ' sample.meta <- utils::read.csv(meta.file)
46
+ # ' track.folder <- system.file("extdata", "RNA-seq", package = "ggcoverage")
47
+ # '
48
+ # ' track.df <- LoadTrackFile(
49
+ # ' track.folder = track.folder, format = "bw",
50
+ # ' meta.info = sample.meta
51
+ # ' )
52
+ # '
53
+ # ' # plot tracks with coloring by 'Group' variable
54
+ # ' ggplot() +
55
+ # ' geom_coverage(data = track.df, facet.key = "Type", group.key = "Group")
56
+ # '
57
+ # ' # plot tracks without coloring by any group
58
+ # ' ggplot() +
59
+ # ' geom_coverage(data = track.df, facet.key = "Type", group.key = NULL)
60
+ # '
61
+ # ' # plot tracks with coloring each facet differently (facet.key == group.key)
62
+ # ' ggplot() +
63
+ # ' geom_coverage(data = track.df, facet.key = "Type", group.key = "Type")
64
+ # '
65
+ # ' # supply your own colors
66
+ # ' ggplot() +
67
+ # ' geom_coverage(
68
+ # ' data = track.df, facet.key = "Type",
69
+ # ' group.key = "Type", color = 1:4,
70
+ # ' facet.color = 1:4
71
+ # ' )
72
+ # '
73
+ # ' # plot tracks together in one panel instead of separately;
74
+ # ' # 'facet.key' is not needed
75
+ # ' ggplot() +
76
+ # ' geom_coverage(
77
+ # ' data = track.df, group.key = "Type",
78
+ # ' plot.type = "joint"
79
+ # ' )
80
+ # '
81
+ # ' # use a custom theme
82
+ # ' ggplot() +
83
+ # ' geom_coverage(data = track.df, facet.key = "Type") +
84
+ # ' theme_bw()
85
+ # '
86
+ # ' # mark a region
87
+ # ' ggplot() +
88
+ # ' geom_coverage(
89
+ # ' data = track.df, facet.key = "Type",
90
+ # ' mark.region = data.frame(
91
+ # ' start = c(21678900,21732001,21737590),
92
+ # ' end = c(21679900,21732400,21737650),
93
+ # ' label=c("M1", "M2", "M3")),
94
+ # ' mark.color = grey(0.4)
95
+ # ' )
96
+ # '
54
97
geom_coverage <- function (data , mapping = NULL , color = NULL , rect.color = NA ,
55
98
single.nuc = FALSE , plot.type = c(" facet" , " joint" ),
56
99
facet.key = " Type" , joint.avg = FALSE , facet.order = NULL ,
@@ -67,32 +110,28 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
67
110
if (is.null(mapping )) {
68
111
if (plot.type == " facet" ) {
69
112
if (! is.null(color )) {
70
- if (length(color ) != length(unique(data [, facet.key ]))) {
71
- warning(" The color you provided is not as long as " , facet.key , " column in data, select automatically!" )
113
+ testcolors <- sapply(color , function (x ) {
114
+ tryCatch(is.matrix(col2rgb(x )),
115
+ error = function (e ) FALSE )
116
+ })
117
+ if (length(color ) < length(unique(data [, group.key ]))) {
118
+ warning(" Fewer colors provided than there are groups in " , group.key , " variable, falling back to default colors" )
72
119
# sample group with same color
73
- tmp.color <- AutoColor(data = data , n = 9 , name = " Set1" , key = group.key )
74
- # change facet key color color
75
- if (facet.key == group.key ) {
76
- fill.color.df <- merge(unique(data [c(facet.key )]), data.frame (color = tmp.color ), by.x = group.key , by.y = 0 )
77
- } else {
78
- fill.color.df <- merge(unique(data [c(facet.key , group.key )]), data.frame (color = tmp.color ), by.x = group.key , by.y = 0 )
79
- }
80
- fill.color <- fill.color.df $ color
81
- names(fill.color ) <- fill.color.df [, facet.key ]
120
+ fill.color <- AutoColor(data = data , n = 9 , name = " Set1" , key = group.key )
82
121
} else {
83
122
fill.color <- color
84
- if (is.null(names( fill.color ))) {
85
- names(fill.color ) <- unique( data [, facet.key ])
86
- }
123
+ }
124
+ if (is.null( names(fill.color ))) {
125
+ names( fill.color ) <- unique( data [, group.key ])
87
126
}
88
127
sacle_fill_cols <- scale_fill_manual(values = fill.color )
89
128
} else {
90
129
sacle_fill_cols <- NULL
91
130
}
92
131
if (! single.nuc ) {
93
- mapping <- aes_string(xmin = " start" , xmax = " end" , ymin = " 0" , ymax = " score" , fill = facet .key )
132
+ mapping <- aes_string(xmin = " start" , xmax = " end" , ymin = " 0" , ymax = " score" , fill = group .key )
94
133
} else {
95
- mapping <- aes_string(x = " start" , y = " score" , fill = facet .key )
134
+ mapping <- aes_string(x = " start" , y = " score" , fill = group .key )
96
135
}
97
136
} else if (plot.type == " joint" ) {
98
137
if (! is.null(color )) {
@@ -189,11 +228,13 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
189
228
facet.formula <- as.formula(paste0(" ~ " , facet.key ))
190
229
if (! single.nuc ) {
191
230
region.rect <- geom_rect(data = data , mapping = mapping , show.legend = F , colour = rect.color )
231
+ ymax.str <- rlang :: as_label(mapping $ ymax )
192
232
} else {
193
233
region.rect <- geom_bar(
194
234
data = data , mapping = mapping , show.legend = F , colour = rect.color ,
195
235
stat = " identity"
196
236
)
237
+ ymax.str <- rlang :: as_label(mapping $ y )
197
238
}
198
239
# prepare facet scale
199
240
if (facet.y.scale == " free" ) {
@@ -214,14 +255,14 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
214
255
plot.ele <- append(plot.ele , sacle_fill_cols )
215
256
}
216
257
217
- # add range text to track
218
- ymax.str <- rlang :: as_label(mapping $ ymax )
219
258
if (range.position == " in" ) {
220
259
data.range <- data %> %
221
260
dplyr :: group_by(.data [[facet.key ]]) %> %
222
- dplyr :: summarise(max_score = max(.data [[ymax.str ]]))
223
- data.range $ max_score <- sapply(data.range $ max_score , CeilingNumber )
224
- data.range $ label <- paste0(" [0, " , data.range $ max_score , " ]" )
261
+ dplyr :: summarise(.groups = " drop_last" ,
262
+ min_score = CeilingNumber(min(.data [[ymax.str ]])),
263
+ max_score = CeilingNumber(max(.data [[ymax.str ]]))
264
+ )
265
+ data.range $ label <- paste0(" [" , data.range $ min_score , " , " , data.range $ max_score , " ]" )
225
266
region.range <- geom_text(
226
267
data = data.range ,
227
268
mapping = aes(x = - Inf , y = Inf , label = label ),
@@ -310,7 +351,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
310
351
311
352
region.mark <- geom_rect(
312
353
data = valid.region.df ,
313
- aes_string(xmin = " start" , xmax = " end" , ymin = " 0 " , ymax = " Inf" ),
354
+ aes_string(xmin = " start" , xmax = " end" , ymin = " -Inf " , ymax = " Inf" ),
314
355
fill = mark.color , alpha = mark.alpha
315
356
)
316
357
plot.ele <- append(plot.ele , region.mark )
0 commit comments