Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function hai_density_qq_plot() #141

Closed
spsanderson opened this issue Jan 11, 2022 · 0 comments
Closed

Add function hai_density_qq_plot() #141

spsanderson opened this issue Jan 11, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented Jan 11, 2022

hai_density_qq_plot <- function(.data, .dist_name_col, .x_col, .y_col,
                                .size = 1, .alpha = 0.382, .interactive = FALSE){
  
  # Tidyeval ----
  dist_name_var <- rlang::enquo(.dist_name_col)
  x_col_var     <- rlang::enquo(.x_col)
  y_col_var     <- rlang::enquo(.y_col)
  size <- as.numeric(.size)
  alpha <- as.numeric(.alpha)
  
  dnv_name <- rlang::quo_name(dist_name_var)
  xcv_name <- rlang::quo_name(x_col_var)
  ycv_name <- rlang::quo_name(y_col_var)
  
  # Checks ----
  if(rlang::quo_is_missing(dist_name_var) | 
     rlang::quo_is_missing(x_col_var) |
     rlang::quo_is_missing(y_col_var)
  ){
    rlang::abort(
    "All parameters must be supplied:
       * .dist_name_col, 
       * .x_col, and
       * .y_col"
    )
  }
  
  if(!is.numeric(alpha) | (alpha > 1) | (alpha < 0)){
    rlang::abort("The .alpha parameter must be a number between 0 and 1")
  }
  
  if(!is.numeric(size) | (size <= 0)){
    rlang::abort("The .size parameter must be a number greater than 0")
  }
  
  # Data setup ----
  data_tbl <- tibble::as_tibble(.data) %>%
    dplyr::ungroup() %>%
    dplyr::select(
      {{dist_name_var}},
      {{x_col_var}},
      {{y_col_var}}
    )
  
  # Plots ----
  plt <- ggplot2::ggplot(
    data = data_tbl,
    mapping = ggplot2::aes_string(
      sample = ycv_name,
      color  = dnv_name,
      group  = dnv_name
    )
  ) +
    ggplot2::stat_qq(size = size, alpha = alpha) +
    ggplot2::stat_qq_line() +
    ggplot2::theme_minimal() +
    ggplot2::labs(
      title = "QQ Plot Comparison",
      color = "Distribution"
    ) +
    ggplot2::theme(legend.position = "bottom")
  
  if(.interactive){
    plt <- plotly::ggplotly(plt)
  }
  
  # Return ----
  return(plt)
}
@spsanderson spsanderson self-assigned this Jan 11, 2022
@spsanderson spsanderson added the enhancement New feature or request label Jan 11, 2022
@spsanderson spsanderson added this to the healthyR.ai 0.0.6 milestone Jan 11, 2022
@spsanderson spsanderson moved this from Todo to In Progress in @spsanderson's Repository Issue Overview Jan 11, 2022
Repository owner moved this from In Progress to Done in @spsanderson's Repository Issue Overview Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

1 participant