nspyrison/spinifex

(complex, low-medium roi) remove duplication in legend (color and shape on 2 lines when mapped to the same)

Closed this issue · 1 comments

## As far as I can see must use do.call on aes_string; and string lists.
#
## Complications: use of '$', duplicates (ie. col and shape =clas)
## Flow:
# 1) Safely extract unique strings to cbind
# 2) cbind all unique get(strings). 
#   - Does it need to get back to global ggtour objects?
# 3) do.call "aes_string" over the list
require("ggplot2")
#> Loading required package: ggplot2
#> Warning: package 'ggplot2' was built under R version 4.1.1
dat <- mtcars
orig_nms <- colnames(dat)
nms <- names(my_aes_args)
#> Error in eval(expr, envir, enclos): object 'my_aes_args' not found
ex <- list("disp", "mpg", col="cyl", shape="cyl")

my_col <- factor(mtcars$cyl)
foo <- function(my_aes_args = list(col = "my_col")){
  ## Initialize
  dat <- mtcars
  idx <- !duplicated(my_aes_args)
  u_args <- my_aes_args[idx]
  orig_nms <- colnames(dat)
  if(any('$' %in% u_args)) attach(dat) ## Make cols available directly
  
  ## Cbind columns to data
  .m <- sapply(seq_along(u_args), function(i){
    assign(u_args[i], get(my_aes_args[[i]]))
    dat <<- cbind(dat, get(nms[i])) ## needs colname
  })
  colnames(dat) <- c(orig_nms, u_args)
  
  ## 
  do.call("aes_string", my_aes_args) ## THIS WOULD work, need apply cbind and name first
}
foo()
#> Error in assign(u_args[i], get(my_aes_args[[i]])): invalid first argument
##### enquo method will fail, because it is predicated on hardcoded map variables.
## WILL NOT WORK CAUSE col, shape alpha need to be hard coded; need do.call
# If your wrapper has a more specific interface with named arguments,
# you need "enquote and unquote":
scatter_by <- function(data, x, y) {
  x <- enquo(x)
  y <- enquo(y)
  ggplot(data) + geom_point(aes(!!x, !!y))
}
scatter_by(mtcars, disp, drat)

Created on 2021-09-29 by the reprex package (v2.0.1)

Fixed in theme_spinifex, with labs(color="", fill="") the label being the same value is key.