Bonus Tissue Gene Expression, 3d and interactive

Bonus Interactive and 3D Plots

library(dslabs)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(tissue_gene_expression)
pca <- prcomp(tissue_gene_expression$x)
tissue_pca_df <- data.frame(
  PC1 = pca$x[,1],
  PC2 = pca$x[,2],
  PC3 = pca$x[,3],
  Tissue = tissue_gene_expression$y
)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
p <- ggplot(tissue_pca_df, aes(x = PC1, y = PC2, color = Tissue)) +
  geom_point(size = 3, alpha = 0.8) +
  labs(title = "Interactive 2D PCA of Gene Expression",
       x = "PC1",
       y = "PC2") +
  theme_minimal()

ggplotly(p)
p3d <- plot_ly(
  tissue_pca_df,
  x = ~PC1, y = ~PC2, z = ~PC3,
  color = ~Tissue,
  colors = "Set1",
  type = "scatter3d",
  mode = "markers"
) %>%
  layout(title = "3D PCA Plot of Gene Expression")

p3d
  • PC1, PC2, and PC3 represent the top 3 components from PCA they explain the most variation in the gene expression matrix.
  • The interactive 2D plot lets users hover over points