-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyueling9-3dpca plot.R
202 lines (131 loc) · 4.57 KB
/
yueling9-3dpca plot.R
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
##系统报错改为英文
Sys.setenv(LANGUAGE = "en")
##禁止转化为因子
options(stringsAsFactors = FALSE)
##清空环境
rm(list=ls())
library(Seurat)
library(scatterplot3d)
setwd("E:/super.lesson/lesson8/")
load("sc.combined.rdata")
table([email protected]$new.celltype.2)
table([email protected]$type)
#setwd("E:/super.lesson/lesson9/")
## Fibroblasts
sc.imm=subset(sc.combined,idents = c("Epithelial","Endothelial","Stem_cell"))
Idents(sc.imm)="orig.ident"
[email protected]$orig.ident=factor([email protected]$orig.ident,levels = c(
"GF_1","GF_2","GF_3","SPF_1","SPF_2","SPF_3","FMT_1","FMT_2","FMT_3"
))
Idents(sc.imm)="orig.ident"
table([email protected])
?AverageExpression
x=AverageExpression(sc.imm ,add.ident="new.celltype.2")
x1=AverageExpression(sc.imm )
x=x$RNA
x1=x1$RNA
pca <- prcomp(t(x))
pca.data=pca$x
rownames(pca.data)
pca.data=as.data.frame(pca.data)
pca.data=pca.data[,1:3]
pca.data$Type = c(rep("GF",9),rep("SPF",9),rep("FMT",9))
s1=strsplit(rownames(pca.data),split = "_",fixed = T)
type=sapply(s1, function(x){x[1]} )
pca.data$cell.type = c(rep(c("Epithelial","Endothelial","Stem_cell"),9 ))
Type.p=c(rep(11,9),rep(16,9),rep(17,9))
cell.type.p = c(rep(c("blue","red","orange"),9 ))
colors.lib <- c("blue","red","orange")
shapes.lib = c(11,16,17)
################插入部分为claude提供的3d作图函数
library(plotly)
plot_3d_pca <- function(pca_data) {
cell_colors <- c("Epithelial" = "blue",
"Endothelial" = "red",
"Stem_cell" = "orange")
type_shapes <- c("GF" = "square-open", # 空心正方形
"SPF" = "triangle-up-open", # 空心三角形
"FMT" = "circle-open") # 空心圆形
p <- plot_ly() %>%
add_trace(
data = pca_data,
x = ~PC1, y = ~PC2, z = ~PC3,
color = ~cell.type,
symbol = ~Type,
colors = cell_colors,
symbols = type_shapes,
type = "scatter3d",
mode = "markers",
marker = list(size = 8)
) %>%
layout(
scene = list(
xaxis = list(title = "PC1"),
yaxis = list(title = "PC2"),
zaxis = list(title = "PC3")
),
title = "3D PCA Plot"
)
return(p)
}
p <- plot_3d_pca(pca.data)
p
###############################################以下部分为华哥的
getwd()
setwd("/Users/yuepen/Downloads/华哥2024单细胞高阶班/资料/11/")
setwd("~/Downloads/华哥2024单细胞高阶班/资料/11/")
# 1. Source the function
source( "/Users/yuepen/Downloads/华哥2024单细胞高阶班/资料/11/huage3Dplot.R" )
source("/Users/yuepen/Downloads/华哥2024单细胞高阶班/资料/11/huage3Dplot.R")
# 2. 3D scatter plot
s3d <- scatterplot3d(pca.data[,c("PC1","PC2","PC3")],
pch = Type.p,color = cell.type.p,
cex.symbols = 1,grid=FALSE, box=FALSE,
main = "3D PCA plot")
legend("topright",
c("Epithelial","Endothelial","Stem_cell"),
fill=c('blue',"red","orange"),
box.col=NA)
legend("topleft",
c('GF','SPF','FMT'),
pch = shapes.lib,
box.col=NA)
# 3. Add grids
addgrids3d(pca.data[,c("PC1","PC2","PC3")], grid = c("xy", "xz", "yz"))
table([email protected]$new.celltype.2)
## Goblet Paneth cells Enteroendocrine
Idents(sc.combined)="new.celltype.3"
sc.imm=subset(sc.combined,idents = c("Goblet","MSC","T_NK"))
Idents(sc.imm)="orig.ident"
x=AverageExpression(sc.imm ,add.ident="new.celltype.3")
x=x$RNA
pca <- prcomp(t(x))
pca.data=pca$x
rownames(pca.data)
pca.data=as.data.frame(pca.data)
pca.data=pca.data[,1:3]
pca.data$Type = c(rep("GF",9),rep("SPF",9),rep("FMT",9))
pca.data$cell.type = c(rep(c("Goblet","Paneth cells","Enteroendocrine"),9 ))
Type.p=c(rep(11,9),rep(16,9),rep(17,9))
cell.type.p = c(rep(c("blue","red","orange"),9 ))
colors.lib <- c("blue","red","orange")
shapes.lib = c(11,16,17)
getwd()
# 1. Source the function
source('plot.3d.R')
# 2. 3D scatter plot
s3d <- scatterplot3d(pca.data[,c("PC1","PC2","PC3")],
pch = Type.p,color = cell.type.p,
cex.symbols = 1,grid=FALSE, box=FALSE,
main = "3D PCA plot")
legend("topright",
c("Goblet","Paneth cells","Enteroendocrine"),
fill=c('blue',"red","orange"),
box.col=NA)
legend("topleft",
c('GF','SPF','FMT'),
pch = shapes.lib,
box.col=NA)
# 3. Add grids
addgrids3d(pca.data[,c("PC1","PC2","PC3")], grid = c("xy", "xz", "yz"))