Nothing
#' @title Plot PCA of cluster characteristics
#'
#' @description This function performs a principal component analysis (PCA) on
#' selected cluster characteristics obtained from \code{\link{postHoc}} and returns
#' a plot of the variable loadings for the first two principal components.
#'
#' @param resposthoc An object of class \code{resposthoc} obtained from the
#' function \code{\link{postHoc}}.
#' @param selectedVar A character vector specifying the variables to include
#' in the PCA. The default is \code{c("TDP", "meanlogFC", "nbPixels", "nbBins",
#' "minHeight", "maxHeight", "stripeScore", "triangleScore")}.
#'
#' @return A \code{ggplot} object displaying the loadings of the first two principal
#' components computed from the selected cluster characteristics.
#'
#' @author Élise Jorge \email{elise.jorge@inrae.fr}\cr
#' Sylvain Foissac \email{sylvain.foissac@inrae.fr}\cr
#' Toby Dylan Hocking \email{toby.hocking@r-project.org}\cr
#' Pierre Neuvial \email{pierre.neuvial@math.univ-toulouse.fr}\cr
#' Nathalie Vialaneix \email{nathalie.vialaneix@inrae.fr}
#'
#' @examples
#' data("pighic")
#' resdiff <- performTest(pighic$data, pighic$conditions)
#' \donttest{res2D <- AggloClust2D(resdiff)
#' if (!is.null(res2D)) {
#' resposthoc <- postHoc(resdiff, res2D$clustering, alpha = 0.05)
#' clusterPCA(resposthoc)
#' }}
#'
#' @export
#'
#' @importFrom dplyr %>%
#' @importFrom dplyr mutate
#' @importFrom dplyr group_by
#' @importFrom dplyr filter
#' @importFrom dplyr select
#' @importFrom dplyr row_number
#' @importFrom dplyr pull
#' @importFrom dplyr ungroup
#' @importFrom stats prcomp
#' @importFrom stats sd
#' @importFrom tidyselect all_of
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 geom_segment
#' @importFrom ggplot2 geom_text
#' @importFrom ggplot2 labs
#' @importFrom ggplot2 theme_bw
#' @importFrom grid arrow
#' @importFrom grid unit
#' @importFrom rlang .data
clusterPCA <- function(resposthoc, selectedVar = c("TDP", "meanlogFC", "nbPixels", "nbBins", "minHeight", "maxHeight", "stripeScore", "triangleScore")) {
df_res <- resposthoc$dfres
## Add column number of pixels per cluster
df_res <- df_res %>%
group_by(.data$clust) %>%
mutate(nbPixels = length(.data$region1))
## Add column number of bins per cluster
df_res <- df_res %>%
group_by(.data$clust) %>%
mutate(nbBins = length(unique(c(.data$region1, .data$region2))))
## Add minHeight column, min distance to diagonal, i.e, min(j - i)
df_res <- df_res %>%
group_by(.data$clust) %>%
mutate(minHeight = min(.data$region2 - .data$region1))
## Add maxHeight column, upper bound of distance to diagonal, i.e, max(j - i + 1)
df_res <- df_res %>%
group_by(.data$clust) %>%
mutate(maxHeight = max(.data$region2 - .data$region1 + 1))
## Prepare data for PCA
df_PCA <- df_res %>%
group_by(.data$clust) %>%
filter(row_number()==1) %>%
ungroup() %>%
select(all_of(c("TDP", "meanlogFC", "nbPixels", "nbBins",
"minHeight", "maxHeight", "stripeScore", "triangleScore")))
## Select for PCA the variables in selectedVar
df_PCA <- df_PCA %>%
select(all_of(c(selectedVar)))
## Transform stripeScore and meanlogFC to absolute values
if("meanlogFC" %in% selectedVar){
df_PCA <- df_PCA %>%
mutate(absmeanlogFC = abs(.data$meanlogFC)) %>%
select(-"meanlogFC")
}
if("stripeScore" %in% selectedVar){
df_PCA <- df_PCA %>%
mutate(absstripeScore = abs(.data$stripeScore)) %>%
select(-"stripeScore")
}
## Remove variables with constant value
zero_sd_cname <- unlist(lapply(colnames(df_PCA), function(cname){
if(stats::sd(pull(df_PCA, cname)) == 0) return(cname)
}))
df_PCA <- df_PCA %>%
select(-all_of(zero_sd_cname))
if(ncol(df_PCA) == 0){
stop("Can't perform PCA: no variable with non-zero variance.")
}
## Perform PCA
pca <- as.data.frame(prcomp(df_PCA, scale. = TRUE, center = TRUE)$rotation)
pca$variable <- rownames(pca)
## Output PC1 and PC2 plot
p <- ggplot(pca, aes(.data$PC1, .data$PC2, label = .data$variable)) +
geom_segment(aes(xend = 0, yend = 0), arrow = arrow(length = unit(0.1, "cm")),
colour = "grey50") +
geom_text(size = 3) +
labs(title = "Variable loadings") +
theme_bw(base_size = 14)
return(p)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.