-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
617fd50
commit b0411d8
Showing
8 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#' Plot the projection from the optimisation data collected from projection pursuit | ||
#' @param dt a data object collected by the projection pursuit guided tour optimisation in \code{tourr} | ||
#' @param data the original data | ||
#' @param cols additional columns to include in the plot | ||
#' @return a ggplot object | ||
#' @examples | ||
#' holes_1d_jellyfish |> get_best() |> plot_projection(data = boa5) | ||
#' @rdname projection | ||
#' @export | ||
plot_projection <- function(dt, data, cols = NULL){ | ||
|
||
cols <- dplyr::syms(cols) | ||
proj_df <- compute_projection(dt, data, cols = cols) | ||
|
||
d <- ncol(proj_df) - 1 - length(cols) | ||
|
||
if (d == 2){ | ||
p <- proj_df |> | ||
ggplot2::ggplot() + | ||
ggplot2::geom_point(ggplot2::aes(x = V1, y = V2)) | ||
} else if (d == 1) { | ||
p <- proj_df |> | ||
ggplot2::ggplot() + | ||
ggplot2::geom_density(ggplot2::aes(x = V1)) | ||
} | ||
|
||
p + ggplot2::facet_wrap(ggplot2::vars(.id)) + | ||
ggplot2::theme_bw() + | ||
ggplot2::theme(aspect.ratio = 1, | ||
axis.ticks = ggplot2::element_blank(), | ||
axis.text = ggplot2::element_blank(), | ||
#axis.title = ggplot2::element_blank(), | ||
panel.grid = ggplot2::element_blank()) | ||
} | ||
|
||
|
||
#' @rdname projection | ||
#' @export | ||
compute_projection <- function(dt, data, cols = NULL){ | ||
|
||
basis_d <- sapply(dt$basis, function(xx) dim(xx)[2], simplify = TRUE) |> unique() | ||
|
||
if (basis_d > 2) { | ||
cli::cli_abort("The basis dimension should be less than 2") | ||
} | ||
|
||
cols <- dplyr::syms(cols) | ||
|
||
suppressWarnings( | ||
dt |> | ||
dplyr::mutate(.id = dplyr::row_number()) |> | ||
dplyr::rowwise() |> | ||
dplyr::mutate(proj = list(tibble::as_tibble(as.matrix(data) %*% basis))) |> | ||
dplyr::select(.id, proj, !!!cols) |> | ||
tidyr::unnest(proj) |> | ||
dplyr::ungroup() | ||
) | ||
} | ||
globalVariables(c("V1", "V2", ".id", "proj")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.