Skip to content

Instantly share code, notes, and snippets.

@diazdc
Created March 14, 2018 16:46
Show Gist options
  • Save diazdc/bfdd7f2d11841d65d62a76bf803181f5 to your computer and use it in GitHub Desktop.
Save diazdc/bfdd7f2d11841d65d62a76bf803181f5 to your computer and use it in GitHub Desktop.
3D Plot for Seurat
counts.seurat <- RunTSNE(counts.seurat,
reduction.use = "pca",
dims.use = 1:6,
dim.embed = 3)
DimPlot(object = counts.seurat, reduction.use = "tsne", dim.1 = 1, dim.2 = 3)
tSNE_1 <- counts.seurat@dr$tsne@cell.embeddings[,1]
tSNE_2 <- counts.seurat@dr$tsne@cell.embeddings[,2]
tSNE_3 <- counts.seurat@dr$tsne@cell.embeddings[,3]
TSNEPlot(counts.seurat)
color_scheme <- as.numeric(1:14)[counts.seurat@ident]
for(i in 1:1521){
if(color_scheme[i] == 1){
color_scheme[i] <- "#F8766D"
}else if(color_scheme[i] == 2){
color_scheme[i] <- "#E38900"
}else if(color_scheme[i] == 3){
color_scheme[i] <- "#C49A00"
}else if(color_scheme[i] == 4){
color_scheme[i] <- "#99A800"
}else if(color_scheme[i] == 5){
color_scheme[i] <- "#53B400"
}else if(color_scheme[i] == 6){
color_scheme[i] <- "#00BC56"
}else if(color_scheme[i] == 7){
color_scheme[i] <- "#00C094"
}else if(color_scheme[i] == 8){
color_scheme[i] <- "#00BFC4"
}else if(color_scheme[i] == 9){
color_scheme[i] <- "#00B6EB"
}else if(color_scheme[i] == 10){
color_scheme[i] <- "#06A4FF"
}else if(color_scheme[i] == 11){
color_scheme[i] <- "#A58AFF"
}else if(color_scheme[i] == 12){
color_scheme[i] <- "#DF70F8"
}else if(color_scheme[i] == 13){
color_scheme[i] <- "#FB61D7"
}else if(color_scheme[i] == 14){
color_scheme[i] <- "#FF66A8"
}else{NULL}
}
library(rgl)
plot3d(x = tSNE_1, y = tSNE_2, z = tSNE_3,
col = color_scheme,
type = "s", size = 0.3, box = FALSE)
rgl::rglwidget("~/Desktop/tSNE_3dplot.html") #save as html
@pangxueyu233
Copy link

beautiful codes, but how did you save a motion graph of 3D Plot for Seurat? for Power point displaying or on other mobile devices ?

@Dragonmasterx87
Copy link

Dragonmasterx87 commented Apr 8, 2019

BEAUTIFUL code, works with seurat v3 as well. Can you please suggest how one can plot gene expression on the 3D plot using your codes? CHEERS!!

06/02/2020:
For readers, check out: 3D Plotting in Seurat

@joaquinhenriquez
Copy link

Hi,

when i run your code it gave
Error: no slot of name "dr" for this object of class "Seurat"
I really do not know what could be

Thanks!

@mrmonocyte
Copy link

@joaquinhenriquez
If you are using Seurat v3 the slot name has changed.
https://satijalab.org/seurat/essential_commands.html

You can use the object[["reduction_type"]]@cell.embeddings[,n] after running your reduction with n.components = 3L

@LineWulff
Copy link

Thanks for the above code - very easy to work with!!
To ease the colouring after clusters I added a colour column to the metadata and coloured by that as follow:

colour_df <- data.frame("cluster"=as.numeric(unique(mcdc.3D@meta.data$res.0.8)[order(unique(mcdc.3D@meta.data$res.0.8))]),
                        "colour"=hue_pal()(length(unique(mcdc.3D@meta.data$res.0.8))))
colour_vec <- NULL
for (cl in as.numeric(mcdc.3D@meta.data$res.0.8)){colour_vec <- append(colour_vec,as.character(colour_df$colour[colour_df$cluster %in% cl]))}
mcdc.3D@meta.data$col0.8 <- colour_vec

plot3d(...,col=mcdc.3D@meta.data$col0.8)

My Seurat object is called mcdc.3D and I have coloured after clusters in res 0.8.
I currently use Seurat 2.4 - so adjustments might be necessary for v3.

@joaquinhenriquez
Copy link

Hi!

Is there a way to highlight a group of cells that express a gene in the 3d plot?

Best,
Joaquin

@diazdc
Copy link
Author

diazdc commented Aug 12, 2019

Hi Joaquin,

I haven't done it myself, but you could do it by passing the expression vector for your gene of interest through colorRampPallete. This would generate a vector of colors that you would assign to color_scheme.

Here's an example if you're using Seurat v3:

exp_vector <- seurat_obj@assays$RNA@data[grep("atoh1a",rownames(seurat_obj)),]
n_cells <- length(colnames(seurat_obj))

breaks <- cut(exp_vector, breaks = seq(min(exp_vector), max(exp_vector),
    len = n_cells), include.lowest = TRUE)

color_scheme <- colorRampPalette(c("white", "blue"))(n_cells)[breaks]

@joaquinhenriquez
Copy link

cool! I finally did it in this way,

gecko252com<- RunTSNE(combined, reduction.use = "tsne", dims.use = 1:6, dim.embed = 3)

DimPlot(object = combined, reduction.use = "tsne", dim.1 = 1, dim.2 = 3)
tSNE_1 <- combined[["tsne"]]@cell.embeddings[,1]
tSNE_2 <- combined[["tsne"]]@cell.embeddings[,2]
tSNE_3 <- combined[["tsne"]]@cell.embeddings[,3]

combined<- SetIdent(object = combined, cells = WhichCells(combined, expression = PITX2>1), value = '1')
combined <- SetIdent(object = combined, cells = WhichCells(combined, expression = PITX2<=1), value = '2')
TSNEPlot(combined)

levels(gecko252com)
Idents(gecko252com)
color_scheme <- as.numeric(1:2)[Idents(combined)]
for(i in 1:2){
if(color_scheme[i] == 1){
color_scheme[i] <- "#F8766D"
}else if(color_scheme[i] == 2){
color_scheme[i] <- "#E38900"
}else{NULL}
}

library(rgl)
open3d()
plot3d(x = tSNE_1, y = tSNE_2, z = tSNE_3,
col = color_scheme,
type = "s", size = 0.3, box = FALSE)
M <- par3d("userMatrix")
if (!rgl.useNULL())
play3d( par3dinterp(time = (0:2)*0.75, userMatrix = list(M,
rotate3d(M, pi/2, 1, 0, 0),
rotate3d(M, pi/2, 0, 1, 0) ) ),
duration = 3 )
movie3d( spin3d(), duration = 5, dir = "C:/Users/ideapad 710s plus i7/Desktop/courses")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment