31
31
# ' @importFrom stats as.formula
32
32
# ' @importFrom ggh4x facet_wrap2 strip_themed elem_list_rect
33
33
# ' @importFrom dplyr group_by summarise
34
- # ' @importFrom dplyr %>%
34
+ # ' @importFrom dplyr %>% filter
35
35
# ' @importFrom ggrepel geom_text_repel
36
36
# ' @importFrom utils tail
37
37
# '
@@ -119,16 +119,16 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
119
119
if (length(color ) < length(unique(data [, group.key ]))) {
120
120
warning(" Fewer colors provided than there are groups in " , group.key , " variable, falling back to default colors" )
121
121
# 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" )
123
123
} else {
124
124
fill.color <- color
125
125
}
126
126
if (is.null(names(fill.color ))) {
127
127
names(fill.color ) <- unique(data [, group.key ])
128
128
}
129
- sacle_fill_cols <- scale_fill_manual(values = fill.color )
129
+ scale_fill_cols <- scale_fill_manual(values = fill.color )
130
130
} else {
131
- sacle_fill_cols <- NULL
131
+ scale_fill_cols <- NULL
132
132
}
133
133
if (! single.nuc ) {
134
134
mapping <- aes_string(xmin = " start" , xmax = " end" , ymin = " 0" , ymax = " score" , fill = group.key )
@@ -140,7 +140,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
140
140
if (length(color ) != length(unique(data [, group.key ]))) {
141
141
warning(" The color you provided is not as long as " , group.key , " column in data, select automatically!" )
142
142
# 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" )
144
144
# change group key color
145
145
color.color.df <- merge(unique(data [c(group.key )]), data.frame (color = tmp.color ), by.x = group.key , by.y = 0 )
146
146
color.color <- color.color.df $ color
@@ -169,7 +169,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
169
169
fill.str.len <- length(unique(data [, fill.str ]))
170
170
if (is.null(color ) | length(color ) != fill.str.len ) {
171
171
# 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" )
173
173
# change color
174
174
fill.color.df <- merge(unique(data [c(fill.str , group.key )]), data.frame (color = tmp.color ), by.x = group.key , by.y = 0 )
175
175
fill.color <- fill.color.df $ color
@@ -180,9 +180,9 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
180
180
names(fill.color ) <- unique(data [, fill.str ])
181
181
}
182
182
}
183
- sacle_fill_cols <- scale_fill_manual(values = fill.color )
183
+ scale_fill_cols <- scale_fill_manual(values = fill.color )
184
184
} else {
185
- sacle_fill_cols <- NULL
185
+ scale_fill_cols <- NULL
186
186
}
187
187
} else if (plot.type == " joint" ) {
188
188
message(" For joint visualization, the mapping should contains start, score, color." )
@@ -191,7 +191,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
191
191
color.str.len <- length(unique(data [, color.str ]))
192
192
if (is.null(color ) | length(color ) != color.str.len ) {
193
193
# 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" )
195
195
# change color
196
196
if (color.str == group.key ) {
197
197
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,
223
223
224
224
# facet color
225
225
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" )
227
227
}
228
228
229
229
# facet formula
@@ -255,8 +255,8 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
255
255
plot.ele <- list (region.rect , region.facet )
256
256
257
257
# color the track
258
- if (! is.null(sacle_fill_cols )) {
259
- plot.ele <- append(plot.ele , sacle_fill_cols )
258
+ if (! is.null(scale_fill_cols )) {
259
+ plot.ele <- append(plot.ele , scale_fill_cols )
260
260
}
261
261
262
262
if (range.position == " in" ) {
@@ -336,36 +336,29 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
336
336
# add rect
337
337
if (! is.null(mark.region )) {
338
338
# get valid mark region
339
- region.start <- data [1 , " start" ]
340
- region.end <- data [nrow(data ), " end" ]
341
- valid.region.list <- list ()
342
- for (r in 1 : nrow(mark.region )) {
343
- if (mark.region [r , " start" ] < = region.end & mark.region [r , " end" ] > = region.start ) {
344
- if (mark.region [r , " end" ] > = region.end ) {
345
- mark.region [r , " end" ] <- region.end
346
- }
347
- if (mark.region [r , " start" ] < = region.start ) {
348
- mark.region [r , " start" ] <- region.start
349
- }
350
- valid.region.list [[r ]] <- mark.region [r , ]
351
- }
352
- }
353
- valid.region.df <- do.call(rbind , valid.region.list ) %> % as.data.frame()
354
- colnames(valid.region.df ) <- colnames(mark.region )
355
-
339
+ region.start <- min(data $ start )
340
+ region.end <- max(data $ end )
341
+ mark.region <- dplyr :: filter(
342
+ mark.region ,
343
+ .data [[" start" ]] > = region.start ,
344
+ .data [[" end" ]] < = region.end
345
+ )
356
346
region.mark <- geom_rect(
357
- data = valid .region.df ,
347
+ data = mark .region ,
358
348
aes_string(xmin = " start" , xmax = " end" , ymin = " -Inf" , ymax = " Inf" ),
359
349
fill = mark.color , alpha = mark.alpha
360
350
)
361
351
plot.ele <- append(plot.ele , region.mark )
362
352
# add rect label
363
353
if (show.mark.label ) {
364
- if (" label" %in% colnames(valid .region.df )) {
354
+ if (" label" %in% colnames(mark .region )) {
365
355
# create mark region label
366
- region.label <- valid .region.df
356
+ region.label <- mark .region
367
357
if (plot.type == " facet" ) {
368
- region.label [, facet.key ] <- facet.order [1 ]
358
+ region.label [, facet.key ] <- factor (
359
+ rep(facet.order [1 ], nrow(mark.region )),
360
+ facet.order
361
+ )
369
362
}
370
363
region.mark.label <- geom_text_repel(
371
364
data = region.label ,
0 commit comments