From 170b0c4e78c75e880a73bb6ad7e3f67b2abc1366 Mon Sep 17 00:00:00 2001
From: Thomas Neitmann
Date: Sun, 26 Apr 2020 17:59:48 +0200
Subject: [PATCH 1/3] Automatically determine `prefix` and `suffix` from
`levels`
---
R/plot_annotation.R | 37 +++++++++++++++++++++++++++++--------
1 file changed, 29 insertions(+), 8 deletions(-)
diff --git a/R/plot_annotation.R b/R/plot_annotation.R
index 43d0d8d..714ef4f 100644
--- a/R/plot_annotation.R
+++ b/R/plot_annotation.R
@@ -91,15 +91,36 @@ ggplot_add.plot_annotation <- function(object, plot, object_name) {
#' @importFrom utils as.roman
recurse_tags <- function(x, levels, prefix, suffix, sep, offset = 1) {
if (length(levels) == 0) return(list(patches = x, tab_ind = offset))
- level <- switch(
- as.character(levels[1]),
- a = letters,
- A = LETTERS,
- "1" = 1:100,
- i = tolower(as.roman(1:100)),
- I = as.roman(1:100),
+
+ levels <- as.character(levels[1])
+ if (grepl("a", levels)) {
+ level <- letters
+ } else if (grepl("A", levels)) {
+ level <- LETTERS
+ } else if (grepl("1", levels)) {
+ level <- 1:100
+ } else if (grepl("i", levels)) {
+ level <- tolower(as.roman(1:100))
+ } else if (grepl("I", levels)) {
+ level <- as.roman(1:100)
+ } else {
stop('Unknown tag type: ', levels[1], call. = FALSE)
- )
+ }
+
+ if (prefix == "" && nchar(levels > 1)) {
+ index <- gregexpr(pattern = as.character(levels), levels)[[1]][1]
+ if (index > 1) {
+ prefix <- substr(levels, 1, index - 1)
+ }
+ }
+
+ if (suffix == "" && nchar(levels > 1)) {
+ index <- gregexpr(pattern = as.character(levels), levels)[[1]][1]
+ if (index > 1) {
+ suffix <- substr(levels, index + 1, nchar(levels))
+ }
+ }
+
patches <- x$patches$plots
tag_ind <- offset
for (i in seq_along(patches)) {
From 8ba3edb8bb639a6e0e3f23c1b602ecba8945a132 Mon Sep 17 00:00:00 2001
From: Thomas Neitmann
Date: Thu, 30 Apr 2020 22:23:39 +0200
Subject: [PATCH 2/3] Avoid using regexps to find substring
---
R/plot_annotation.R | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)
diff --git a/R/plot_annotation.R b/R/plot_annotation.R
index 714ef4f..77fc3ae 100644
--- a/R/plot_annotation.R
+++ b/R/plot_annotation.R
@@ -108,17 +108,15 @@ recurse_tags <- function(x, levels, prefix, suffix, sep, offset = 1) {
}
if (prefix == "" && nchar(levels > 1)) {
- index <- gregexpr(pattern = as.character(levels), levels)[[1]][1]
+ index <- find_index_substr(levels, level[1])
if (index > 1) {
prefix <- substr(levels, 1, index - 1)
}
}
if (suffix == "" && nchar(levels > 1)) {
- index <- gregexpr(pattern = as.character(levels), levels)[[1]][1]
- if (index > 1) {
- suffix <- substr(levels, index + 1, nchar(levels))
- }
+ index <- find_index_substr(levels, level[1])
+ suffix <- substr(levels, index + 1, nchar(levels))
}
patches <- x$patches$plots
@@ -192,3 +190,20 @@ annotate_table <- function(table, annotation) {
z = -Inf, name = 'background')
table
}
+
+find_index_substr <- function(string, substring) {
+ index <- 1
+ match <- FALSE
+ while (index <= nchar(string) && !match) {
+ if (substr(string, index, index) == substring) {
+ match <- TRUE
+ } else {
+ index <- index + 1
+ }
+ }
+ if (match) {
+ return(index)
+ } else {
+ return(NA)
+ }
+}
From 53e7902b4aed571d3661344c41473a5f67ae195c Mon Sep 17 00:00:00 2001
From: Thomas Neitmann
Date: Thu, 30 Apr 2020 22:39:20 +0200
Subject: [PATCH 3/3] Convert `level` to `character`
---
R/plot_annotation.R | 37 ++++++++++++++++++-------------------
1 file changed, 18 insertions(+), 19 deletions(-)
diff --git a/R/plot_annotation.R b/R/plot_annotation.R
index 77fc3ae..315a547 100644
--- a/R/plot_annotation.R
+++ b/R/plot_annotation.R
@@ -108,14 +108,14 @@ recurse_tags <- function(x, levels, prefix, suffix, sep, offset = 1) {
}
if (prefix == "" && nchar(levels > 1)) {
- index <- find_index_substr(levels, level[1])
+ index <- find_index_substr(levels, as.character(level[1]))
if (index > 1) {
prefix <- substr(levels, 1, index - 1)
}
}
if (suffix == "" && nchar(levels > 1)) {
- index <- find_index_substr(levels, level[1])
+ index <- find_index_substr(levels, as.character(level[1]))
suffix <- substr(levels, index + 1, nchar(levels))
}
@@ -152,6 +152,22 @@ recurse_tags <- function(x, levels, prefix, suffix, sep, offset = 1) {
tag_ind = tag_ind
)
}
+find_index_substr <- function(string, substring) {
+ index <- 1
+ match <- FALSE
+ while (index <= nchar(string) && !match) {
+ if (substr(string, index, index) == substring) {
+ match <- TRUE
+ } else {
+ index <- index + 1
+ }
+ }
+ if (match) {
+ return(index)
+ } else {
+ return(NA)
+ }
+}
#' @importFrom ggplot2 ggplot labs ggplotGrob
#' @importFrom gtable gtable_add_rows gtable_add_cols
#' @importFrom grid unit
@@ -190,20 +206,3 @@ annotate_table <- function(table, annotation) {
z = -Inf, name = 'background')
table
}
-
-find_index_substr <- function(string, substring) {
- index <- 1
- match <- FALSE
- while (index <= nchar(string) && !match) {
- if (substr(string, index, index) == substring) {
- match <- TRUE
- } else {
- index <- index + 1
- }
- }
- if (match) {
- return(index)
- } else {
- return(NA)
- }
-}