diff --git a/DESCRIPTION b/DESCRIPTION index 0fc53e60..767b0dfe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ Description: We provide a tidy grammar of population genetics, facilitating License: GPL (>= 3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Depends: R (>= 3.0.2), dplyr, diff --git a/R/as_q_matrix.R b/R/as_q_matrix.R index f9fbfdb2..fdccb13b 100644 --- a/R/as_q_matrix.R +++ b/R/as_q_matrix.R @@ -26,17 +26,18 @@ as_q_matrix <- function(x){ #' Takes a `q_matrix` object, which is a matrix, and returns a tidied tibble. #' #' @param x A Q matrix object (as returned by LEA::Q()). -#' @param gen_tbl An associated gen_tibble +#' @param data An associated tibble (e.g. a [`gen_tibble`]), with the individuals in the same order as the data used to +#' generate the Q matrix #' @param ... not currently used #' @return A tidied tibble #' @export -tidy.q_matrix <- function(x, gen_tbl, ...){ +tidy.q_matrix <- function(x, data, ...){ rlang::check_dots_empty() q_tbl <- x %>% tibble::as_tibble() %>% - dplyr::mutate(id = gen_tbl$id, + dplyr::mutate(id = data$id, # @TODO we should get this from the grouped tibble, not hardcode it! - group = gen_tbl$population) + group = data$population) q_tbl <- q_tbl %>% tidyr::pivot_longer(cols = dplyr::starts_with(".Q"), names_to = "q", values_to = "percentage") @@ -63,12 +64,13 @@ tidy.q_matrix <- function(x, gen_tbl, ...){ #' produces a list of tidied tibbles ready to plot. #' #' @param x the name of a directory containing .Q files -#' @param gen_tbl An associated gen_tibble +#' @param data An associated tibble (e.g. a [`gen_tibble`]), with the individuals in the same order as the data used to +#' generate the Q matrix #' @returns a list of `q_matrix` objects to plot #' #' @export -read_q_matrix_list <- function(x, gen_tbl){ +read_q_matrix_list <- function(x, data){ files <- list.files(x, pattern = "\\.Q$", full.names = TRUE) @@ -82,7 +84,7 @@ read_q_matrix_list <- function(x, gen_tbl){ matrix_list <- matrix_list[order(sapply(matrix_list, ncol))] # Tidy each - matrix_list <- lapply(matrix_list, function(x) tidy(x, gen_tbl = gen_tbl)) + matrix_list <- lapply(matrix_list, function(x) tidy(x, gen_tbl = data)) matrix_list } @@ -90,8 +92,9 @@ read_q_matrix_list <- function(x, gen_tbl){ #' Autoplots for `q_matrix` objects #' -#' @param object A Q matrix object (as returned by `as_q_matrix`). -#' @param gen_tbl An associated gen_tibble +#' @param object A Q matrix object (as returned by [as_q_matrix()]). +#' @param data An associated tibble (e.g. a [`gen_tibble`]), with the individuals in the same order as the data used to +#' generate the Q matrix #' @param annotate_group Boolean determining whether to annotate the plot with the #' group information #' @param ... not currently used. @@ -99,17 +102,17 @@ read_q_matrix_list <- function(x, gen_tbl){ #' #' @export -autoplot.q_matrix <- function(object, gen_tbl = NULL, annotate_group = TRUE, ...){ +autoplot.q_matrix <- function(object, data = NULL, annotate_group = TRUE, ...){ rlang::check_dots_empty() K <- ncol(object) - if (is.null(gen_tbl)) { + if (is.null(data)) { q_tbl <- as.data.frame(object) q_tbl$id <- 1:nrow(q_tbl) q_tbl <- q_tbl %>% tidyr::pivot_longer(cols = dplyr::starts_with(".Q"), names_to = "q", values_to = "percentage") } else { - q_tbl <- tidy(object, gen_tbl) + q_tbl <- tidy(object, data) } plt <- ggplot2::ggplot(q_tbl, ggplot2::aes(x = .data$id, @@ -121,7 +124,7 @@ autoplot.q_matrix <- function(object, gen_tbl = NULL, annotate_group = TRUE, ... theme_distruct() + scale_fill_distruct() if (annotate_group){ - if (is.null(gen_tbl)){ + if (is.null(data)){ warning("no annotation possible if 'gen_tbl' is NULL") } else { plt <- plt + annotate_group_info(q_tbl) diff --git a/man/autoplot.q_matrix.Rd b/man/autoplot.q_matrix.Rd index 8c2cef26..4cfd932b 100644 --- a/man/autoplot.q_matrix.Rd +++ b/man/autoplot.q_matrix.Rd @@ -4,14 +4,15 @@ \alias{autoplot.q_matrix} \title{Autoplots for \code{q_matrix} objects} \usage{ -\method{autoplot}{q_matrix}(object, gen_tbl = NULL, annotate_group = TRUE, ...) +\method{autoplot}{q_matrix}(object, data = NULL, annotate_group = TRUE, ...) } \arguments{ -\item{object}{A Q matrix object (as returned by \code{as_q_matrix}).} +\item{object}{A Q matrix object (as returned by \code{\link[=as_q_matrix]{as_q_matrix()}}).} -\item{gen_tbl}{An associated gen_tibble} +\item{data}{An associated tibble (e.g. a \code{\link{gen_tibble}}), with the individuals in the same order as the data used to +generate the Q matrix} -\item{annotate_group}{Boolean determining whether to annonate the plot with the +\item{annotate_group}{Boolean determining whether to annotate the plot with the group information} \item{...}{not currently used.} diff --git a/man/read_q_matrix_list.Rd b/man/read_q_matrix_list.Rd index 199d5142..7521e0b2 100644 --- a/man/read_q_matrix_list.Rd +++ b/man/read_q_matrix_list.Rd @@ -4,12 +4,13 @@ \alias{read_q_matrix_list} \title{Tidy ADMXITURE output files into plots} \usage{ -read_q_matrix_list(x, gen_tbl) +read_q_matrix_list(x, data) } \arguments{ \item{x}{the name of a directory containing .Q files} -\item{gen_tbl}{An associated gen_tibble} +\item{data}{An associated tibble (e.g. a \code{\link{gen_tibble}}), with the individuals in the same order as the data used to +generate the Q matrix} } \value{ a list of \code{q_matrix} objects to plot diff --git a/man/tidy.q_matrix.Rd b/man/tidy.q_matrix.Rd index ce2a1b4b..03200d84 100644 --- a/man/tidy.q_matrix.Rd +++ b/man/tidy.q_matrix.Rd @@ -4,12 +4,13 @@ \alias{tidy.q_matrix} \title{Tidy a Q matrix} \usage{ -\method{tidy}{q_matrix}(x, gen_tbl, ...) +\method{tidy}{q_matrix}(x, data, ...) } \arguments{ \item{x}{A Q matrix object (as returned by LEA::Q()).} -\item{gen_tbl}{An associated gen_tibble} +\item{data}{An associated tibble (e.g. a \code{\link{gen_tibble}}), with the individuals in the same order as the data used to +generate the Q matrix} \item{...}{not currently used} } diff --git a/vignettes/a03_example_clustering_and_dapc.Rmd b/vignettes/a03_example_clustering_and_dapc.Rmd index 9ee436c4..4bf6af85 100644 --- a/vignettes/a03_example_clustering_and_dapc.Rmd +++ b/vignettes/a03_example_clustering_and_dapc.Rmd @@ -341,7 +341,7 @@ head(q_mat) We can quickly plot it with ```{r} -autoplot(q_mat,gen_tbl = anole_gt) +autoplot(q_mat, data = anole_gt) ``` We can tidy our q matrix, and attach the population and id data from our original gen_tibble, returning it