Created
November 21, 2024 22:12
-
-
Save abikoushi/3a484a13b42cd98325e8420d0d0ba83d to your computer and use it in GitHub Desktop.
Comparison plots for parallel coordinate vs. UMAP
This file contains hidden or 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
library(Matrix) | |
library(readr) | |
library(tidyr) | |
library(dplyr) | |
library(ggplot2) | |
library(umap) | |
library(patchwork) | |
#https://www.weizmann.ac.il/sites/3CA/breast | |
cells <- read_csv("./data/Data_Chung2017_Breast/Cells.csv") | |
path <- "./data/Data_Chung2017_Breast/Exp_data_TPM.mtx" | |
mat <- readMM(path) | |
rm_wch <- which(apply(mat, MARGIN = 1, FUN = function(x)all(x==0))) | |
mat <- mat[-rm_wch, ] | |
#ca005 <- rgb(0,0,0,0.05) | |
system.time({ | |
res20_svd <- svd(log1p(as.matrix(mat)), nu = 20, nv=20) | |
}) | |
# user system elapsed | |
# 15.191 0.067 15.254 | |
V <- res20_svd$v | |
colnames(V) <- paste0("PC",1:20) | |
df <- data.frame(V) %>% | |
mutate(cell_type=cells$cell_type, id=1:n()) %>% | |
pivot_longer(PC1:PC20) %>% | |
mutate(name = factor(name, levels=paste0("PC",1:20))) | |
p1 <- ggplot(df, aes(x=name,y=value,colour=cell_type, group=id))+ | |
geom_line(alpha=0.1, position = position_jitter(height = 0,width = 0.1, seed = 123))+ | |
scale_colour_brewer(palette = "Set2")+ | |
guides(colour=guide_legend(override.aes = list(alpha=1, linewidth=2)))+ | |
theme_classic()+ | |
ggtitle("PCA") | |
ures_svd <- umap(res20_svd$v) | |
udf_svd <- data.frame(ures_svd$layout, celltype=factor(cells$cell_type)) | |
up2 <- ggplot(udf_svd, aes(x=X1, y=X2, colour=celltype))+ | |
geom_point(alpha=0.3)+ | |
scale_colour_brewer(palette = "Set2")+ | |
guides(colour=guide_legend(override.aes = list(alpha=1, linewidth=2)))+ | |
theme_classic()+ | |
ggtitle("PCA -> UMAP")+labs(x="UMAP1", y="UMAP2") | |
p1 / up2 | |
ggsave("pcp_umap.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment