Skip to content

Commit

Permalink
parts of #7 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
raysinensis committed Sep 29, 2024
1 parent 2e11637 commit 6a91070
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
8 changes: 5 additions & 3 deletions R/color_repel.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @param seed sampling randomization seed
#' @param col colour or fill in ggplot
#' @param autoswitch try to switch between colour and fill automatically
#' @param layer layer to detect color, defaults to first
#' @param out_orig output the original colors as named vector
#' @param out_worst output the worst combination instead of best
#' @examples
Expand All @@ -30,14 +31,15 @@ color_repel <- function(g,
seed = 34,
col = "colour",
autoswitch = TRUE,
layer = 1,
out_orig = FALSE,
out_worst = FALSE) {
g <- check_patchwork(g)

if (verbose) {
message("extract original colors...")
}
temp <- check_colour_mapping(g, col = col, return_col = TRUE, autoswitch = autoswitch)
temp <- check_colour_mapping(g, col = col, return_col = TRUE, autoswitch = autoswitch, layer = layer)
col <- temp[["col"]]
cols <- temp[["cols"]]
g2 <- ggplot2::ggplot_build(g)
Expand Down Expand Up @@ -76,10 +78,10 @@ color_repel <- function(g,
if (verbose) {
message("extract plot distances...")
}
if (all(c("x", "y") %in% colnames(g2$data[[1]]))) {
if (all(c("x", "y") %in% colnames(g2$data[[layer]]))) {
em <- dplyr::select(g2$data[[1]], x, y)
# clustering info
clust <- as.character(g2$data[[1]][[col]])
clust <- as.character(g2$data[[layer]][[col]])
clust <- as.character(as.numeric(factor(clust, levels = orig_cols)))
if (downsample == "chull") {
res <- by_cluster_chull(em, clust, xcol = "x", ycol = "y")
Expand Down
6 changes: 3 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ get_labs <- function(g) {
}
}

check_colour_mapping <- function(g, col = "colour", return_col = FALSE, autoswitch = TRUE) {
check_colour_mapping <- function(g, col = "colour", return_col = FALSE, autoswitch = TRUE, layer = 1) {
g2 <- ggplot2::ggplot_build(g)
cols <- dplyr::arrange(g2$data[[1]], group)
cols <- dplyr::arrange(g2$data[[layer]], group)
cols <- unique(cols[[col]])
if (length(cols) <= 1) {
if (!autoswitch) {
Expand All @@ -346,7 +346,7 @@ check_colour_mapping <- function(g, col = "colour", return_col = FALSE, autoswit
} else {
col <- "fill"
}
cols <- dplyr::arrange(g2$data[[1]], group)
cols <- dplyr::arrange(g2$data[[layer]], group)
cols <- unique(cols[[col]])
}
if (return_col) {
Expand Down
6 changes: 4 additions & 2 deletions R/wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param nsamp how many random sampling color combinations to test, default 50000
#' @param seed sampling randomization seed
#' @param autoswitch try to switch between colour and fill automatically
#' @param layer layer to detect color, defaults to first
#' @param out_orig output the original colors as named vector
#' @param out_worst output the worst combination instead of best
#' @param repel_label whether to add centroid labels with ggrepel
Expand All @@ -33,6 +34,7 @@ gg_color_repel <- function(g = ggplot2::last_plot(),
nsamp = 50000,
seed = 34,
autoswitch = TRUE,
layer = 1,
out_orig = FALSE,
out_worst = FALSE,
repel_label = FALSE,
Expand All @@ -48,13 +50,13 @@ gg_color_repel <- function(g = ggplot2::last_plot(),
downsample = downsample,
nsamp = nsamp, seed = seed,
sim = sim, severity = severity,
autoswitch = autoswitch,
autoswitch = autoswitch, layer = layer,
out_orig = out_orig,
out_worst = out_worst
)

if (autoswitch) {
col <- check_colour_mapping(g, col = col, autoswitch = autoswitch)
col <- check_colour_mapping(g, col = col, autoswitch = autoswitch, layer = layer)
}
.f <- paste0("ggplot2:::scale_", col, "_manual")

Expand Down
3 changes: 3 additions & 0 deletions man/color_repel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/gg_color_repel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6a91070

Please sign in to comment.