s6juncheng / ggpval

Add statistical test or annotation to your ggplot2 plots,
https://s6juncheng.github.io/ggpval/
45 stars 9 forks source link

Settings `pairs = NULL` by default. #11

Closed dbrookeUAB closed 4 years ago

dbrookeUAB commented 4 years ago

I had a dataset with only two groups and I kept getting

> add_pval(p1)
Error in wilcox.test.formula(data = data_2_test, response ~ group__, ...) : 
  grouping factor must have exactly 2 levels

It took me a stupid amount of time to figure out that pairs = list(c(1,2), c(1,3) is the default and I need to make sure to set pairs explicitly every time.

You could place

if(is.null(pairs)){
  total_groups <- length(unique(ggplot_obj$data[, get(ggpval:::get_in_parenthesis(as.character(ggplot_obj$mapping[1])))]))

  if(total_groups==2){
    pairs <- list(c(1,2))
  } else {
    pairs <- lapply(2:total_groups, function(x) c(1,x))
  }

right in the beginning with pairs = NULL in the arguments. For simple plots, you could just use add_pval(plot) for simplicity.

s6juncheng commented 4 years ago

Looks good to me, now merge it, thanks!

dbrookeUAB commented 4 years ago

Coooool!!! This is the first time I've contributed to someone's package.