[314984]: / c_VisualizationScript / Visulz_bulkRNA_PCA.R

Download this file

71 lines (53 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# MESSAGE -----------------------------------------------------------------
#
# author: Yulin Lyu
# email: lvyulin@pku.edu.cn
#
# require: R whatever
#
# ---
# 1. Load packages --------------------------------------------------------
setwd("exampleData/RNA")
# grammar
library(tidyverse)
library(magrittr)
library(glue)
library(data.table)
# analysis
library(DESeq2)
library(irlba)
# graphics
library(ggplot2)
library(ggrepel)
library(ggsci)
library(scales)
# 2. Load data ------------------------------------------------------------
vsd <- readRDS("mid/vsd.rds")
vsdMtx <- assay(vsd)
diffData <- fread("mid/ES_vs_Fib.DEG.csv")
colnames(diffData)[1] <- "gene"
# 3. Plot -----------------------------------------------------------------
diffData[, type := "ns"][]
diffData[is.na(padj), padj := 1][]
diffData[log2FoldChange > 3 & padj < 0.05, type := "up"][log2FoldChange < -3 & padj < 0.05, type := "down"][]
usedGene <- diffData[type %in% c("up", "down"), gene]
PCAdata <- prcomp_irlba(t(vsdMtx[usedGene, ]), n = 3, scale. = T)
s <- summary(PCAdata)
s <- s$importance[2, ] %>% round(4)
plotData <- as.data.table(PCAdata$x)
plotData[, id := colnames(vsdMtx)][, type := rep(c("Fib", "CiPS", "ES"), each = 2)][]
usedCol <- pal_npg()(10)[c(1, 4, 3)] %>% set_names(c("Fib", "CiPS", "ES"))
ggplot(plotData, aes(x = PC1, y = PC2)) +
geom_point(aes(color = type), size = 3, shape = 16, show.legend = F) +
geom_text_repel(aes(label = id)) +
scale_color_manual(values = usedCol) +
labs(
x = str_c("PC1 (", s[1] * 100, "%)"),
y = str_c("PC2 (", s[2] * 100, "%)")) +
coord_fixed() +
theme(
panel.background = element_blank(),
panel.grid = element_blank(),
panel.border = element_rect(fill = NA)
)
ggsave("plot/PCA.png", width = 6, height = 2)