seurat color palette

Created on 23 Dec 2017  路  2Comments  路  Source: satijalab/seurat

Hello,

Is there a way to retrieve the colors that TSNEPlot for each group? I would like to use the same exact colors for labeling my cells in another context.

Thank you!
Andrea

Most helpful comment

Hi Andrea,

You can use ggplot2's ggplot_build function to deconstruct a tSNE plot into the data used to make said plot.

> p <- Seurat::TSNEPlot(object, do.return = TRUE) # Generate the tSNE plot, but save it as an object
> pbuild <- ggplot2::ggplot_build(p) # Use ggplot_build to deconstruct the ggplot object
> pdata <- pbuild$data[[1]] # Pull the data used for the plot

The colors used, in hexadecimal, are in the colour column of pdata, the groups in the group column.

If you want a vector of the colors used you can do the following:

> pdata <-  pdata[order(pdata$group), ] # Order the plot data by group
> ucols <- unique(pdata$colour) # Get a vector of unique colors
> names(ucols) <- unique(pdata$group) # Add the groups to the vector of colors as names

This will leave you with a named vector of unique colors used, where the names are the groups extracted from the tSNE plot.

All 2 comments

Hello Andrea,

If the plotted colours are the default colours of ggplot2, you can get these colours using the hue_pal() function of the scales packages in R. This comes in handy if you have a very large number of clusters. Otherwise, you can define your custom colours using scales_colour_manual(). Below is a combination of both options:

# Load the "scales" package
require(scales)

# Create vector with levels of object@ident
identities <- levels(object@ident)

# Create vector of default ggplot2 colors
my_color_palette <- hue_pal()(length(identities))

# Plot the tSNE plot with the default ggplot2 colors
TSNEPlot(object = object, do.return = T) + 
  scale_color_manual(values = my_color_palette)

The colour codes can be found in my_color_palette.

Hope this helps!

Best,
Leon

Hi Andrea,

You can use ggplot2's ggplot_build function to deconstruct a tSNE plot into the data used to make said plot.

> p <- Seurat::TSNEPlot(object, do.return = TRUE) # Generate the tSNE plot, but save it as an object
> pbuild <- ggplot2::ggplot_build(p) # Use ggplot_build to deconstruct the ggplot object
> pdata <- pbuild$data[[1]] # Pull the data used for the plot

The colors used, in hexadecimal, are in the colour column of pdata, the groups in the group column.

If you want a vector of the colors used you can do the following:

> pdata <-  pdata[order(pdata$group), ] # Order the plot data by group
> ucols <- unique(pdata$colour) # Get a vector of unique colors
> names(ucols) <- unique(pdata$group) # Add the groups to the vector of colors as names

This will leave you with a named vector of unique colors used, where the names are the groups extracted from the tSNE plot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

whitleyo picture whitleyo  路  3Comments

milanmlft picture milanmlft  路  3Comments

rajasreemenon picture rajasreemenon  路  3Comments

RuiyangLiu94 picture RuiyangLiu94  路  3Comments

igordot picture igordot  路  3Comments