Skip to content

Instantly share code, notes, and snippets.

@kieranrcampbell
Created April 10, 2018 18:26
Show Gist options
  • Save kieranrcampbell/2e9bd0af7769d7c2902030747857dd9e to your computer and use it in GitHub Desktop.
Save kieranrcampbell/2e9bd0af7769d7c2902030747857dd9e to your computer and use it in GitHub Desktop.
#' Plot a CNV heatmap
#' \code{cnv_data} must have the following columns:
#'
#' - start (start position of each region)
#' - chr (chromosome)
#' - single_cell_id (id of each cell)
#' - clone (clone to which each cell is assigned)
#' - copy_number (copy number of each clone in region)
#'
#' @export
plot_cnv_heatmap <- function(cnv_data) {
cnv_cols <- c("0" = "#2166ac",
"1" = "#92c5de",
"2" = "grey80",
"3" = "#f4a582",
"4" = "#d6604d",
"5" = "#b2182b",
"6+" = "#67001f")
chr_levels <- c(as.character(1:23), "X", "Y")
cnv_data$chr <- factor(cnv_data$chr, levels = chr_levels)
cnv_data$copy_number[cnv_data$copy_number >= 6] <- "6+"
cnv_data$copy_number <- as.character(cnv_data$copy_number)
ggplot(cnv_data, aes(x = start, y = single_cell_id, fill = copy_number)) +
geom_raster() +
facet_grid(clone ~ chr, scales = "free", space = "free", switch = "both") +
scale_fill_manual(values = cnv_cols, name = "Copy Number") +
labs(x = "Chromosome", y = "Clone") +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
strip.background = element_rect(fill = 'white'),
legend.position = "bottom")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment