#' Plot kNN graph.
#'
#' Plot the kNN graph generated by buildKNN and stored at CellRouter@@graph. The
#' graph could also be generated by the findClusters function.
#'
#' @param object CellRouter object.
#' @param assay.type character; the type of data to use.
#' @param reduction.type character; the reduced dimension space used to
#' visualize the kNN graph: tsne, pca, dc or custom.
#' @param column.ann character; column in the metadata table used to annotate
#' the kNN graph. For example, clusters, sorted cell populations...
#' @param column.color character; column in the metadata table corresponding to
#' the color used to annotate the kNN graph. Should correspond to the metadata
#' in column.ann.
#' @param dims.use numeric; dimensions to plot.
#'
#' @return ggplot2; plot.
#'
#' @export
#' @docType methods
#' @rdname plotKNN-methods
setGeneric("plotKNN", function(object, assay.type='RNA',
reduction.type = "tsne", column.ann,
column.color, dims.use = c(1,2))
standardGeneric("plotKNN"))
#' @rdname plotKNN-methods
#' @aliases plotKNN
setMethod("plotKNN",
signature = "CellRouter",
definition = function(object, assay.type,
reduction.type, column.ann, column.color,
dims.use){
h <- object@graph$network
matrix <- slot(object,
reduction.type)$cell.embeddings[igraph::V(h)$name,
dims.use]
colors <- unique(
slot(object, 'assays')[[assay.type]]@sampTab[[column.color]])
names(colors) <- unique(as.vector(
slot(object, 'assays')[[assay.type]]@sampTab[[column.ann]]))
g <- ggnetwork::ggnetwork(h, layout = as.matrix(matrix), na.rm = TRUE)
g2 <- ggplot2::ggplot(g, ggplot2::aes(x = x, y = y, xend = xend,
yend = yend)) +
ggnetwork::geom_edges(alpha = 0.3) +
ggnetwork::geom_nodes(ggplot2::aes(color = factor(comms)), size=1) +
ggnetwork::theme_blank() +
ggplot2::scale_color_manual("", values=colors) +
ggplot2::guides(col = ggplot2::guide_legend(direction="vertical",
keywidth = 0.75,
keyheight = 0.85,
override.aes = list(size = 3)))
return(g2)
}
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.