Commit d0b5ce18 authored by Jun Zhao's avatar Jun Zhao
Browse files

update plot functions

parent 9ab4a351
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ import(RANN)
import(Seurat)
import(cowplot)
import(ggplot2)
import(ggrepel)
import(glmnet)
import(reticulate)
import(scales)
+31 −14
Original line number Diff line number Diff line
#' @import ggplot2
#' @import cowplot
#' @import ggrepel
NULL


@@ -16,14 +17,13 @@ NULL
#' @return a ggplot object
#' @export
#'
plotCellScore <- function(X, score, cell.col = c("blue","white","red"), size = 0.5, alpha = 1){
plotCellScore <- function(X, score, cell.col = c("blue","white","red"), size = 0.5, alpha = 1, shape = 16){
  # Add colnames for X
  colnames(X) <- c("Dim1","Dim2")

  # Plot cells with labels
  myggplot <- ggplot() + theme_cowplot() +
    geom_point(data = data.frame(Dim1 = X[,1], Dim2 = X[,2], Score = score),
               aes(x = Dim1, y = Dim2, col = Score), size = size, alpha = alpha) +
  myggplot <- ggplot(data = data.frame(Dim1 = X[,1], Dim2 = X[,2], Score = score)) + theme_cowplot() +
    geom_point(aes(x = Dim1, y = Dim2, col = Score), shape = shape, size = size, alpha = alpha) +
    scale_color_gradientn(colours = cell.col)

  return(myggplot)
@@ -49,11 +49,11 @@ plotDAsite <- function(X, site.list, size = 0.5, cols = NULL){
  }

  myggplot <- ggplot() + theme_cowplot() +
    geom_point(data = data.frame(X), aes(Dim1, Dim2), col = "gray", size = size) +
    geom_point(data = data.frame(X), aes(Dim1, Dim2), shape = 16, col = "gray", size = size) +
    geom_point(
      data = data.frame(X[unlist(site.list),]),
      aes(Dim1, Dim2, col = as.factor(site.label[unlist(site.list)])),
      size = size
      shape = 16, size = size
    ) + theme(legend.position = "none")

  if(!is.null(cols) & length(cols) == length(site.list)){
@@ -77,19 +77,24 @@ plotDAsite <- function(X, site.list, size = 0.5, cols = NULL){
#' @param alpha numeric between 0 to 1, dot opacity for each cell, default 1
#' @param do.label a logical value indicating whether to add text to mark each cell label
#' @param label.size numeric, size of text labels, default 4
#' @param label.repel a logical value indicating whether to repel the labels to avoid overlapping, default False
#' @param label.plot cell labels to add text annotations, default NULL, add text for all labels
#'
#' @return a ggplot object
#' @export
#'
plotCellLabel <- function(X, label, cell.col = NULL, size = 0.5, alpha = 1, do.label = T, label.size = 4){
plotCellLabel <- function(
  X, label, cell.col = NULL, size = 0.5, alpha = 1, shape = 16,
  do.label = T, label.size = 4, label.repel = F, label.plot = NULL
){
  # Add colnames for X
  colnames(X) <- c("Dim1","Dim2")

  # Plot cells with labels
  myggplot <- ggplot() + theme_cowplot() +
    geom_point(data = data.frame(Dim1 = X[,1], Dim2 = X[,2], Group = label, stringsAsFactors = F),
               aes(x = Dim1, y = Dim2, col = Group), size = size, alpha = alpha) +
    guides(colour = guide_legend(override.aes = list(size=3,alpha=1), title = NULL))
  myggplot <- ggplot(data = data.frame(Dim1 = X[,1], Dim2 = X[,2], Group = label, stringsAsFactors = F)) +
    theme_cowplot() +
    geom_point(aes(x = Dim1, y = Dim2, col = Group), shape = shape, size = size, alpha = alpha) +
    guides(colour = guide_legend(override.aes = list(size=3,alpha=1,shape=19), title = NULL))

  # Change cell color
  if(!is.null(cell.col)){
@@ -98,12 +103,24 @@ plotCellLabel <- function(X, label, cell.col = NULL, size = 0.5, alpha = 1, do.l

  # Label text
  if(do.label){
    if(is.null(label.plot)){
      mylabels <- unique(label)
    } else {
      mylabels <- label.plot
    }
    labeldim1 <- by(X[,1], INDICES = label, FUN = median)[mylabels]
    labeldim2 <- by(X[,2], INDICES = label, FUN = median)[mylabels]

    if(!label.repel){
      myggplot <- myggplot +
        annotate("text", x = labeldim1, y = labeldim2, label = as.character(mylabels), size = label.size)
    } else {
      myggplot <- myggplot +
        geom_text_repel(
          data = data.frame(x = labeldim1, y = labeldim2, text = mylabels), aes(x,y, label = text),
          segment.colour = NA, size = label.size
        )
    }
  }

  return(myggplot)
+6 −1
Original line number Diff line number Diff line
@@ -5,7 +5,8 @@
\title{Plot cell labels}
\usage{
plotCellLabel(X, label, cell.col = NULL, size = 0.5, alpha = 1,
  do.label = T, label.size = 4)
  shape = 16, do.label = T, label.size = 4, label.repel = F,
  label.plot = NULL)
}
\arguments{
\item{X}{matrix, 2D embedding of each cell for the plot}
@@ -21,6 +22,10 @@ plotCellLabel(X, label, cell.col = NULL, size = 0.5, alpha = 1,
\item{do.label}{a logical value indicating whether to add text to mark each cell label}

\item{label.size}{numeric, size of text labels, default 4}

\item{label.repel}{a logical value indicating whether to repel the labels to avoid overlapping, default False}

\item{label.plot}{cell labels to add text annotations, default NULL, add text for all labels}
}
\value{
a ggplot object
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
\title{Plot a score for each cell}
\usage{
plotCellScore(X, score, cell.col = c("blue", "white", "red"),
  size = 0.5, alpha = 1)
  size = 0.5, alpha = 1, shape = 16)
}
\arguments{
\item{X}{matrix, 2D embedding of each cell for the plot}