R/LAPP.plot.tSNE.R

#' Plot LAPP tSNE
#'
#' \code{LAPP.plot.tSNE()} takes in a list object returned by LAPP.tSNE() and plots it.
#' Uses ggplot2.
#'
#' @param tsne modified PCA object returned by LAPP.tsne(). Can take up to 8 different HLA alleles.
#'
#' @param type How the PCA should be plotted. Either 'points' for scatterplot or
#' 'density' for an automated 2D KDE plot (default).
#'
#' @param colors list of colors for coloring different HLA in the plot. Defaults to a colorblind palette with 8 colors.
#'
#' @param clust.dict optional argument for clustering HLAs together for coloring.
#'
#' @export

LAPP.plot.tSNE = function(tsne, type = "density",
                          colors = c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7"),
                          clust.dict= NA) {

  # workable form
  tsne.df = data.frame(HLA = tsne$HLA, dim1 = tsne$Y[, 1], dim2 = tsne$Y[, 2])

  # if clustering HLA
  if(!is.na(clust.dict[1])){
    tsne.df$HLA= clust.dict[as.character(tsne.df$HLA)]
  }

  # remove items with NA for HLA / not defined in clust.dict
  tsne.df= tsne.df[!is.na(tsne.df$HLA),]

  # plot limits (For density plots)
  tsne.max.d1 = max(tsne.df$dim1) * 1.1
  tsne.max.d2 = max(tsne.df$dim2) * 1.1

  tsne.min.d1 = min(tsne.df$dim1) * 1.1
  tsne.min.d2 = min(tsne.df$dim2) * 1.1

  # default colors from:
  # colorblind palette from http://jfly.iam.u-tokyo.ac.jp/color/
  # colors = c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

  palette = scale_color_manual(values = colors)


  # empty plot
  tsne.plot = ggplot()

  # labels and design settings for PCA plot
  tsne.settings=  ggplot() + coord_cartesian(xlim = c(tsne.min.d1, tsne.max.d1), ylim = c(tsne.min.d2, tsne.max.d2)) +
    scale_x_continuous(limits = c(tsne.min.d1, tsne.max.d1)) +
    scale_y_continuous(limits = c(tsne.min.d2, tsne.max.d2)) +
    scale_alpha_continuous(limits = c(0, 1e-05)) +
    labs(title = paste0("2D t-SNE (", length(levels(as.factor(tsne$HLA))),
                        " HLA groups, ", tsne$nfeats, " features)"),
         x = paste0("t-SNE dimension 1"),
         y = paste0("t-SNE dimension 2")) +
    theme_bw() +
    palette +
    guides(color = guide_legend(label.position = "bottom",
                                title.theme = element_text(face = "bold", angle = 0),
                                label.theme = element_text(angle = 0),
                                override.aes = list(size = 3)))

  # show points
  if (type == "points") {

    # scramble for nicer 'points' plotting
    set.seed(129)
    tsne.df = tsne.df[sample(1:dim(tsne.df)[1], dim(tsne.df)[1]), ]


    tsne.plot = tsne.settings +
      geom_point(data = tsne.df, mapping = aes(x = dim1, y = dim2, color = HLA), size = 2, alpha = 0.5)

      # show densities generated by automated 2D kernel density estimation
  } else if (type == "density") {

    tsne.plot = tsne.settings +
      geom_density2d(data = tsne.df, mapping = aes(x = dim1, y = dim2, color = HLA), size = 1, alpha = 0.8)

  }
  return(tsne.plot)
}
ash129/LAPP documentation built on May 10, 2019, 1:52 p.m.