Recherche de tag: interactive plots


Visualisations d'ACP interactives et colorées ! [R]

31.07.2018     gdevailly      r PCA ACP plotly interactive plots 

  Visualiser des ACP (PCA in english) est une tâche assez commune, mais qui peut-être laborieuse si vous êtes pointilleux comme moi ou ZaZo0o (cf https://bioinfo-fr.net/representer-facilement-une-acp-avec-r-et-ggplot2).

Je vous présente ici une méthode pour faire des ACP interactives (avec zoom, masquage des groupes, noms des points en mouseover, etc.).

Il vous faut installer dplyr, cowplot et plotly depuis CRAN.
library(cowplot)
library(plotly)
library(dplyr)

plotPCA <- function(
    my_pca,
    colorby = NULL,
    xaxs = "PC1", yaxs = "PC2",
    title = NULL,
    palette = function(x) rainbow(x, s = 0.6),
    continuous_color = FALSE,
    ...
) {
    
    # obtaining % of variance explained by each axis
    varxaxs <- (summary(my_pca)$importance["Proportion of Variance", xaxs] * 100) %>% round(digits = 1)
    varyaxs <- (summary(my_pca)$importance["Proportion of Variance", yaxs] * 100) %>% round(digits = 1)
    
    myplot <- tibble(
        myxaxs = my_pca$x[, xaxs],
        myyaxs = my_pca$x[, yaxs],
        texts = rownames(my_pca$x),
        colors = if (is.null(colorby)) {NA} else {colorby}
    ) %>%
        ggplot(aes(x = myxaxs, y = myyaxs, color = colors, text = texts)) +
        geom_point() +
        labs(x = paste0(xaxs, " (", varxaxs, "%)"), y = paste0(yaxs, " (", varyaxs, "%)"), title = title, color = NULL) +
        background_grid()
    
    if (continuous_color) {
        myplot <- myplot + scale_color_gradientn(colors = palette(64))
    } else {
        myplot <- myplot + scale_color_manual(values = palette(n_distinct(colorby)))
    }
    
    ggplotly(myplot, tooltip = "text", ...)
    
}

# generating data for the example
metadata <- tibble(
    group = rep(c("group1", "group2", "group3"), each = 50),
    names = paste("sample", 1:150),
    latent_variable = c(
        rnorm(100) + 4,
        rnorm(50)
    )
)
my_data <- matrix(rnorm(150 * 15), nrow = 150)
my_data[1:100, 1:10] <- my_data[1:50, 1:10] + 3 # PC1 axis differences
my_data[1:50, 11:15] <- my_data[1:50, 11:15] + 2 # PC2 differences 
rownames(my_data) <- metadata$names

# basic example (click on legend to hide/display specific groups):
plotPCA(prcomp(my_data), colorby = metadata$group)

# changing axis and title
plotPCA(prcomp(my_data), colorby = metadata$group, xaxs = "PC2", yaxs = "PC3", title = "PC1 not shown")

# changing color palette:
plotPCA(prcomp(my_data), colorby = metadata$group, palette = viridis::viridis)

# continuous colors:
plotPCA(prcomp(my_data), colorby = metadata$latent_variable, palette = viridis::viridis, continuous_color = TRUE)

# saving as html:
p <- plotPCA(prcomp(my_data), colorby = metadata$group)
htmlwidgets::saveWidget(p, "my_great_pca.html", selfcontained = TRUE)


0/5 - [0 rating]