R/plot_marker.R

Defines functions plot_marker

Documented in plot_marker

#' Prints marker bar and lines of proteins for correlation matrix
#'
#' @param data cor.object
#' @param k number of clusters
#' @param proteins protein accession IDs
#' @param x1 thickness of group bar
#' @param width width of protein bars
#' @param print print plot
#' @param save save plot
#' @param name.clusters should names of clusters be printed
#'
#' @return
#' @export
#'
#' @importFrom reshape2 melt
#' @import ggplot2
#'
#'
plot_marker <- function(data, k = 2, proteins, x1 = 1, width = 3, name.clusters = T, print = T, save = F) {


  marker.line <- data.frame(matrix(NA, nrow = x1 + 1, ncol = length(data[["dendrogram"]]$labels)))

  colnames(marker.line) <- data[["dendrogram"]]$labels[data[["dendrogram"]]$order]

  marker.line[1:x1,] <- ct(data[["dendrogram"]], k = k)[colnames(marker.line)]

  if(hasArg(proteins)) {

    positions <- match(proteins, colnames(marker.line))

    proteins <- proteins[!is.na(positions)]

    positions <- positions[!is.na(positions)]

    marker.line[1:x1, positions] <- 0

  }

  if(name.clusters) {

    cluster.position <- c()

    for(i in 1:k) {

      cluster.position <- c(cluster.position, ceiling(median(which(marker.line[1,] == i))))

    }

  }



  marker.line.melt <- reshape2::melt(cbind(row.names(marker.line), marker.line), id.vars = 1)
  colnames(marker.line.melt) <- c("X", "Y", "value")

  p <- ggplot(marker.line.melt, aes(X, Y)) +
    geom_tile(aes(fill = value)) +
    scale_fill_gradientn(colors = c("green", "purple"), limits = c(1, 10), na.value = "white") +
    theme(axis.title.x = element_blank(),
          axis.text.x = element_blank(),
          axis.ticks.x = element_blank(),
          axis.title.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.text.y = element_blank(),
          legend.position = "none")

  if(hasArg(proteins)) {
    p <- p + annotate("text", x = x1 + 0.75, y = positions, label = raw.data[[1]][proteins, "gene"])
  }

  if(name.clusters) {
    p <- p + annotate("text", x = x1, y = cluster.position, label = 1:k, check_overlap = T)
  }

  if(print) {
    print(p)
  }

  if(save) {
    ggsave("./test.png", p)
  }

}
nicohuttmann/htmnanalysis documentation built on Dec. 6, 2020, 3:02 a.m.