This is the first demo version of integration of ggcyto and tsne. To install:

devtools::install_github("RGLab/ggcyto/tsne")

Load the gated data

library(ggcyto)
gs <- load_gs(system.file("extdata/tbdata", package = "BioC2015OpenCyto"))
gs <- gs[1:4]

Add cytokine marginal gates

library(openCyto)
for(marker in c("IFNg", "IL2", "IL22", "IL4", "IL17a", "TNFa"))
  add_pop(gs, marker, parent = "CD4+", dims = marker, gating_method = "tailgate")

We will use all the marginal gates for tsne

nodes <- getChildren(gs[[1]], "CD4+", path = 2)
nodes
## [1] "CD4+/IFNg"  "CD4+/IL2"   "CD4+/IL22"  "CD4+/IL4"   "CD4+/IL17a"
## [6] "CD4+/TNFa"

Firstly, create ggcyto object as usual (except omitting mapping here since it is always goingt to be x and y in tsne space)

p <- ggcyto(gs, subset = "CD4+") 
p <-  p + facet_grid(known_response~Peptide)

Then add stat_tsne layer to ggcyto object

p <- p + stat_tsne(nodes = nodes, nEvents = 2e3, groupBy = "Peptide")

Note that here we can subsample nEvents from each sample to speed up tsen run At this point, tsne won’t be evalued until the inital plotting is requested.

Here we first plot the polyfunctional tsne plots.

p + geom_point(aes(color = poly), data = function(x) x[degree >3,])
## getting total cell counts from parent gate CD4+
## after grouping by 'Peptide', all groups will at least 2000cells.
## subsampling complete ! recomputing...
## generating event masks
## ........
##  input matrix has2323rows...
## input matrix has2323rows after filtering for cells of degree >0
## starting tSNE run at Fri Feb 10 17:02:58 2017
## completed tSNE run atFri Feb 10 17:03:25 2017!

Keep in mind that the first plot can take some time due to the tsne computation.

But the subsequent plots will be fast since ggcyto will simply fetch the cached tsne data directly from ggcyto object. Here we can see the degree of functionality plot

p <- p + scale_color_gradientn(colours = rev(RColorBrewer::brewer.pal(11, "Spectral")))
p + geom_point(aes(color = degree))

And we can also plot any marginal marker’s signal in tsne space

p <- p + scale_color_gradientn(colours = rev(RColorBrewer::brewer.pal(11, "Spectral")), trans = "log10")
p + geom_point(aes(color = IFNg), size = 1.5, alpha = 0.5)

p + geom_point(aes(color = IL2), size = 1.5, alpha = 0.5)