Switch to unified view

a b/R/DIscBIO-generic-plotLabelstSNE.R
1
#' @title tSNE map with labels
2
#' @description Visualizing k-means or model-based clusters using tSNE maps
3
#' @param object \code{DISCBIO} class object.
4
#' @rdname plotLabelstSNE
5
#' @importFrom graphics text
6
#' @return Plot containing the ID of the cells in each cluster
7
setGeneric("plotLabelstSNE", function(object) {
8
  standardGeneric("plotLabelstSNE")
9
})
10
11
#' @rdname plotLabelstSNE
12
#' @export
13
setMethod(
14
  "plotLabelstSNE",
15
  signature = "DISCBIO",
16
  definition = function(object) {
17
    # ======================================================================
18
    # Validating
19
    # ======================================================================
20
    ran_k <- length(object@tsne) > 0
21
    ran_m <- length(object@MBtsne) > 0
22
    if (ran_k) {
23
      Clusters <- object@kmeans$kpart
24
      x <- object@tsne
25
    } else if (ran_m) {
26
      Clusters <- object@MBclusters$clusterid
27
      x <- object@MBtsne
28
    } else {
29
      stop("run comptsne before plotLabelstSNE")
30
    }
31
    # ======================================================================
32
    # Plotting
33
    # ======================================================================
34
    ClustersFactor <- as.factor(Clusters)
35
    ClustersFactor <- gsub("1", "black", ClustersFactor)
36
    ClustersFactor <- gsub("2", "blue", ClustersFactor)
37
    ClustersFactor <- gsub("3", "green", ClustersFactor)
38
    ClustersFactor <- gsub("4", "red", ClustersFactor)
39
    ClustersFactor <- gsub("5", "yellow", ClustersFactor)
40
    ClustersFactor <- gsub("6", "gray", ClustersFactor)
41
    COL <- ClustersFactor
42
    labels <- names(object@ndata)
43
    plot(
44
      x,
45
      xlab = "Dim 1",
46
      ylab = "Dim 2",
47
      pch = 20,
48
      cex = .5,
49
      col = "lightgrey"
50
    )
51
    text(
52
      x[, 1],
53
      x[, 2],
54
      labels,
55
      cex = .7,
56
      col = COL
57
    )
58
  }
59
)