Skip to content

Commit e146f3d

Browse files
committed
fix: mark region boundaries, closes #39
1 parent b416a04 commit e146f3d

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

R/geom_coverage.R

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' @importFrom stats as.formula
3232
#' @importFrom ggh4x facet_wrap2 strip_themed elem_list_rect
3333
#' @importFrom dplyr group_by summarise
34-
#' @importFrom dplyr %>%
34+
#' @importFrom dplyr %>% filter
3535
#' @importFrom ggrepel geom_text_repel
3636
#' @importFrom utils tail
3737
#'
@@ -336,34 +336,24 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
336336
# add rect
337337
if (!is.null(mark.region)) {
338338
# 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+
)
356346
region.mark <- geom_rect(
357-
data = valid.region.df,
347+
data = mark.region,
358348
aes_string(xmin = "start", xmax = "end", ymin = "-Inf", ymax = "Inf"),
359349
fill = mark.color, alpha = mark.alpha
360350
)
361351
plot.ele <- append(plot.ele, region.mark)
362352
# add rect label
363353
if (show.mark.label) {
364-
if ("label" %in% colnames(valid.region.df)) {
354+
if ("label" %in% colnames(mark.region)) {
365355
# create mark region label
366-
region.label <- valid.region.df
356+
region.label <- mark.region
367357
if (plot.type == "facet") {
368358
region.label[, facet.key] <- facet.order[1]
369359
}

0 commit comments

Comments
 (0)