-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptional_3D_plotting2.Rmd
More file actions
116 lines (95 loc) · 3.88 KB
/
Optional_3D_plotting2.Rmd
File metadata and controls
116 lines (95 loc) · 3.88 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
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
---
title: "Practical: 3D plotting 2"
subtitle: "Transcriptome Analysis Workshop"
author: "Adam Cribbs"
date: "24/01/2022"
output:
html_document:
theme: cosmo
toc: yes
---
```{r, out.width = "40%", echo=FALSE}
htmltools::img(src = knitr::image_uri(file.path("logo.png")),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:10px;',
width='300')
```
```{r}
#install.packages('plotly')
library(plotly)
library(Seurat)
```
```{r}
yourseuratobject <- readRDS("seurat_object.rds")
yourseuratobject <- RunTSNE(yourseuratobject,
reduction.use = "pca",
dims.use = 1:10,
dim.embed = 3)
tsne_1 <- yourseuratobject[["tsne"]]@cell.embeddings[,1]
tsne_2 <- yourseuratobject[["tsne"]]@cell.embeddings[,2]
tsne_3 <- yourseuratobject[["tsne"]]@cell.embeddings[,3]
```
# Prepare a dataframe for cell plotting
```{r}
plot.data <- FetchData(object = yourseuratobject, vars = c("tSNE_1", "tSNE_2", "tSNE_3", "seurat_clusters"))
```
```{r}
# Make a column of row name identities (these will be your cell/barcode names)
plot.data$label <- paste(rownames(plot.data))
# Plot your data, in this example my Seurat object had 21 clusters (0-20)
plot_ly(data = plot.data,
x = ~tSNE_1, y = ~tSNE_2, z = ~tSNE_3,
color = ~seurat_clusters,
colors = c("lightseagreen",
"gray50",
"darkgreen",
"red4",
"red",
"turquoise4",
"black",
"yellow4",
"royalblue1",
"lightcyan3",
"peachpuff3",
"khaki3",
"gray20",
"orange2",
"royalblue4",
"yellow3",
"gray80",
"darkorchid1",
"lawngreen",
"plum2",
"darkmagenta"),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2), # controls size of points
text=~label, #This is that extra column we made earlier for which we will use
hoverinfo="text") #When you visualize your plotly object, hovering your mouse pointer over a point shows cell names
```
```{r}
# Say you wanto make a gene-expression 3D plot, where you can plot gene expression against a color scale
# Here using the same seurat object as above, we extract gene expression information for beta-actin 'ACTB'
# create a dataframe
plotting.data <- FetchData(object = yourseuratobject, vars = c("tSNE_1", "tSNE_2", "tSNE_3", "ACTB"))
# Say you want change the scale, so that every cell having an expression >1 will be one color
# Basically, you are re-adjusting the scale here, so that any cell having a certain expression
# will light up on your 3D plot
# First make another column in your dataframe, where all values above 1 are re-assigned a value of 1
# This information is stored in the 'changed' column of your dataframe
plotting.data$changed <- ifelse(test = plotting.data$ACTB <1, yes = plotting.data$ACTB, no = 1)
# Add the label column, so that now the column has 'cellname-its expression value'
plotting.data$label <- paste(rownames(plotting.data)," - ", plotting.data$ACTB, sep="")
# Plot your data, in this example my Seurat object had 21 clusters (0-20), and cells express a gene called ACTB
plot_ly(data = plotting.data,
x = ~tSNE_1, y = ~tSNE_2, z = ~tSNE_3,
color = ~changed, # you can just run this against the column for the gene as well using ~ACTB, the algorith will automatically scale in that case based on maximal and minimal values
opacity = .5,
colors = c('darkgreen', 'red'),
type = "scatter3d",
mode = "markers",
marker = list(size = 5, width=2),
text=~label,
hoverinfo="text"
)
```