Skip to content

Commit

Permalink
fix plotting order
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Dec 12, 2024
1 parent 4fc0007 commit 84df5e8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
24 changes: 19 additions & 5 deletions R/q_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,21 @@ augment.q_matrix <- function(x, data = NULL, ...) {
#' generate the Q matrix
#' @param annotate_group Boolean determining whether to annotate the plot with the
#' group information
#' @param reorder_within_groups Boolean determining whether to reorder the individuals within each group based
#' on their ancestry proportion (note that this is not advised if you are making multiple plots, as you would get
#' a different order for each plot!). If TRUE, `annotate_group` must also be TRUE.
#' @param ... not currently used.
#' @returns a barplot of individuals, coloured by ancestry proportion
#' @name autoplot_q_matrix
#' @export
autoplot.q_matrix <- function(object, data = NULL, annotate_group = TRUE, ...){
autoplot.q_matrix <- function(object, data = NULL, annotate_group = TRUE, reorder_within_groups = FALSE, ...){

rlang::check_dots_empty()
# test that if reorder_within_groups is TRUE, annotate_group should also be TRUE
if (reorder_within_groups & !annotate_group){
stop("If reorder_within_groups is TRUE, annotate_group should also be TRUE")

Check warning on line 263 in R/q_matrix.R

View check run for this annotation

Codecov / codecov/patch

R/q_matrix.R#L263

Added line #L263 was not covered by tests
}

K <- ncol(object)
# create dataset if we don't have a gen_tibble
if (is.null(data)) {
Expand All @@ -269,17 +277,23 @@ autoplot.q_matrix <- function(object, data = NULL, annotate_group = TRUE, ...){
if ("group" %in% names(attributes(object))){
q_tbl$group <- rep(attr(object, "group"), each=nrow(q_tbl)/length(attr(object, "group")))
}
browser()
} else { # if we have the info from the gen_tibble
q_tbl <- tidy(object, data)
}
# if we have a grouping variable and we plan to use it, then reorder by it
q_tbl$id <- 1:nrow(q_tbl)
if (("group" %in% names(q_tbl)) && annotate_group){
q_tbl <- q_tbl %>%
dplyr::arrange(.data$group, .data$id)
}
# now reset the id to the new order
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") %>%
dplyr::mutate(percentage = as.numeric(.data$percentage))

# resort data if we have a grouping variable and we plan to use it
if (("group" %in% names(q_tbl))&&annotate_group){

# if we reorder within group
if (("group" %in% names(q_tbl)) && reorder_within_groups){
q_tbl <- q_tbl %>%
dplyr::group_by(.data$group, .data$id) %>%
#dplyr::arrange(.data$group, .data$id) %>%
Expand Down
12 changes: 11 additions & 1 deletion man/autoplot_q_matrix.Rd

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

9 changes: 6 additions & 3 deletions tests/testthat/test_gt_admixture.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ test_that("run admixture as multiple runs", {

# test the reorder of q matrices
anole_adm_cv_reorder <- gt_admix_reorder_q(anole_adm_cv)
# TODO check how ordering is done automatically when plotting
autoplot(anole_adm_cv, type = "barplot", k=3,run = 1, annotate_group=FALSE)
autoplot(anole_adm_cv_reorder, type = "barplot", k=3,run = 1)
# check plot ordering
unord_plot <- autoplot(anole_adm_cv, type = "barplot", k=3,run = 1, annotate_group=FALSE)
ord_plot <- autoplot(anole_adm_cv_reorder, type = "barplot", k=3,run = 1, annotate_group=TRUE)
reord_plot <- autoplot(anole_adm_cv_reorder, type = "barplot", k=3,run = 1, annotate_group=TRUE)
expect_identical (ord_plot, reord_plot)
expect_false (identical (unord_plot, ord_plot))
})

0 comments on commit 84df5e8

Please sign in to comment.