Skip to content

Commit 938d56b

Browse files
committed
fix: replace ceilingnumber by pretty to format Y scale, closes #18
1 parent d4bdec6 commit 938d56b

File tree

8 files changed

+147
-75
lines changed

8 files changed

+147
-75
lines changed

R/geom_coverage.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
#' @return Layers of ggplot2.
2727
#' @importFrom ggplot2 aes_string scale_fill_manual geom_rect geom_text aes geom_step
2828
#' @importFrom rlang .data
29-
#' @importFrom grDevices colorRampPalette
29+
#' @importFrom grDevices colorRampPalette col2rgb
3030
#' @importFrom RColorBrewer brewer.pal
3131
#' @importFrom rlang as_label
3232
#' @importFrom stats as.formula
3333
#' @importFrom ggh4x facet_wrap2 strip_themed elem_list_rect
3434
#' @importFrom dplyr group_by summarise
3535
#' @importFrom magrittr %>%
3636
#' @importFrom ggrepel geom_text_repel
37+
#' @importFrom utils tail
3738
#'
3839
#' @export
3940
#' @examples
@@ -259,8 +260,8 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
259260
data.range <- data %>%
260261
dplyr::group_by(.data[[facet.key]]) %>%
261262
dplyr::summarise(.groups = "drop_last",
262-
min_score = CeilingNumber(min(.data[[ymax.str]])),
263-
max_score = CeilingNumber(max(.data[[ymax.str]]))
263+
min_score = pretty(.data[[ymax.str]])[1],
264+
max_score = tail(pretty(.data[[ymax.str]]), 1)
264265
)
265266
data.range$label <- paste0("[", data.range$min_score, ", ", data.range$max_score, "]")
266267
region.range <- geom_text(

R/geom_protein.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
#' @export
3636
#'
3737
#' @examples
38-
#' # library(ggplot2)
39-
#' # library(ggcoverage)
40-
#' # coverage.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.xlsx", package = "ggcoverage")
41-
#' # fasta.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.fasta", package = "ggcoverage")
42-
#' # protein.id = "sp|
43-
#' # ggplot() +
44-
#' # geom_peptide(coverage.file = coverage.file, fasta.file = fasta.file, protein.id = protein.id)
38+
#' library(ggplot2)
39+
#' library(ggcoverage)
40+
#' coverage.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.xlsx", package = "ggcoverage")
41+
#' fasta.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.fasta", package = "ggcoverage")
42+
#' protein.id = "sp|P02769|ALBU_BOVIN"
43+
#' ggplot() +
44+
#' geom_protein(coverage.file = coverage.file, fasta.file = fasta.file, protein.id = protein.id)
4545
geom_protein <- function(coverage.file, fasta.file, protein.id, XCorr.threshold = 2,
4646
confidence = "High", contaminant = NULL, remove.na = TRUE,
4747
color = "grey", mark.bare = TRUE, mark.color = "red", mark.alpha = 0.5,
@@ -187,7 +187,7 @@ geom_protein <- function(coverage.file, fasta.file, protein.id, XCorr.threshold
187187
# range position
188188
if (range.position == "in") {
189189
# prepare range
190-
max.abundance <- CeilingNumber(max(coverage.final$abundance))
190+
max.abundance <- max(pretty(coverage.final$abundance))
191191
abundance.range <- data.frame(label = paste0("[0, ", scales::scientific(max.abundance, digits = 2), "]"))
192192
range.text <- geom_text(
193193
data = abundance.range,

R/theme_ggcoverage.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ theme_coverage <- function(space = 0.2) {
2020
axis.title = element_blank()
2121
),
2222
ggplot2::annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf, size = rel(1)),
23-
scale_y_continuous(expand = expansion(mult = c(0))),
23+
scale_y_continuous(
24+
limits = ~ c(pretty(.x)[1], tail(pretty(.x), 1)),
25+
expand = expansion(mult = c(0))
26+
),
2427
scale_x_continuous(labels = scales::comma, expand = c(0, 0))
2528
)
2629
}
@@ -39,7 +42,7 @@ theme_coverage <- function(space = 0.2) {
3942
theme_coverage2 <- function(space = 0.2) {
4043
list(
4144
scale_y_continuous(
42-
limits = ~ c(0, CeilingNumber(max(.x))),
45+
limits = ~ range(pretty(.x)),
4346
breaks = ~ .x[2],
4447
expand = expansion(mult = c(0))
4548
),
@@ -484,7 +487,7 @@ theme_cnv <- function(x.range, margin.len) {
484487
)
485488
}
486489

487-
# theme for ggprotein: suitable for range position is in
490+
# theme for ggprotein: suitable for range.position = "in"
488491
#' Theme for geom_protein.
489492
#'
490493
#' @return List of layers.
@@ -508,7 +511,7 @@ theme_protein <- function() {
508511
)
509512
}
510513

511-
# theme for ggprotein: suitable for range position is out
514+
# theme for ggprotein: suitable for range.position = "out"
512515
#' Theme for geom_protein.
513516
#'
514517
#' @return List of layers.
@@ -520,7 +523,7 @@ theme_protein <- function() {
520523
theme_protein2 <- function() {
521524
list(
522525
scale_y_continuous(
523-
limits = ~ c(0, CeilingNumber(max(.x)), digits = 2),
526+
limits = ~ range(pretty(.x)),
524527
breaks = ~ .x[2],
525528
expand = expansion(mult = c(0)),
526529
labels = function(x) format(x, scientific = TRUE, digits = 2)

R/utils.R

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,6 @@ AutoColor <- function(data, n, name, key) {
6464
return(fill.color)
6565
}
6666

67-
# ceiling for number bigger than zero, floor for number smaller than zero
68-
CeilingNumber <- function(x, digits = 2) {
69-
if (x == 0) {
70-
"0"
71-
} else if (abs(x) >= 10^6) {
72-
formatC(x, format = "e", digits = 2)
73-
} else if (abs(x) >= 10000) {
74-
formatC(round(x), format = "f", digits = 0)
75-
} else if (abs(x) >= 1) {
76-
if ((x %% floor(x)) != 0) {
77-
formatC(x, format = "f", digits = 2)
78-
} else {
79-
formatC(x, format = "f", digits = 0)
80-
}
81-
} else {
82-
formatC(x, format = "f", digits = 2)
83-
}
84-
}
85-
8667
# create aa plot dataframe with padding offset
8768
AAPadding <- function(len, offset = 0, aa.seq) {
8869
aa.seq.full <- rep(aa.seq, each = 3)

man/geom_base.Rd

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

man/geom_coverage.Rd

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

man/geom_protein.Rd

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

man/ggcoverage.Rd

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

0 commit comments

Comments
 (0)