## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(echo = TRUE, fig.width = 7, fig.height=5)

if (!isTRUE(as.logical(Sys.getenv("NOT_CRAN")))) {
    # on CRAN: return NULL with message
    readRDS <- function(file) {
        origin <- if (is.character(file)) {
            file
        } else if (inherits(file, "connection")) {
            tryCatch(summary(file)$description, error = function(e) "connection object")
        } else {
            paste(deparse(substitute(file)), collapse = " ")
        }
        
        handler <- function(e) {
            message("Could not download example data from ", origin,
                    " (", conditionMessage(e), "). Skipping this example.")
            NULL
        }
        tryCatch({
            base::readRDS(file)
        }, error = handler, warning = handler)
    }
}

## ----message=FALSE------------------------------------------------------------
library(mascarade)
library(data.table)
library(ggplot2)
library(colorrepel)

## -----------------------------------------------------------------------------
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/pbmc3k_umap.rds"))

## ----eval=!is.null(example)---------------------------------------------------
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster)) + 
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               

## ----eval=!is.null(example)---------------------------------------------------
ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster)) + 
    fancyMask(maskTable, ratio=1, linewidth = 0) +
    theme_classic() + theme(legend.position = "none")

## -----------------------------------------------------------------------------
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/pbmc3k_tsne.rds"))

## ----eval=!is.null(example)---------------------------------------------------
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=tSNE_1, y=tSNE_2)) + 
    geom_point(aes(color=cluster)) + 
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               

## ----eval=!is.null(example)---------------------------------------------------
ggplot(data, aes(x=tSNE_1, y=tSNE_2)) + 
    geom_point(aes(color=cluster)) + 
    fancyMask(maskTable, ratio=1, linewidth = 0) +
    theme_classic() + theme(legend.position = "none")

## -----------------------------------------------------------------------------
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/aya.rds"))

## ----eval=!is.null(example), message=FALSE------------------------------------
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster), size=0.5) + 
    scale_color_repel() +
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               

## -----------------------------------------------------------------------------
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/chiajung1.rds"))

## ----eval=!is.null(example), message=FALSE------------------------------------
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster), size=0.1) + 
    scale_color_repel() +
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               

## -----------------------------------------------------------------------------
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/chiajung2.rds"))

## ----eval=!is.null(example), message=FALSE------------------------------------
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster)) + 
    scale_color_repel() +
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               

## -----------------------------------------------------------------------------
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/vshitov.rds"))

## ----eval=!is.null(example), fig.height=7, fig.width=8, message=FALSE---------
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP1, y=UMAP2)) + 
    geom_point(aes(color=cluster), size=0.1) + 
    scale_color_repel() +
    fancyMask(maskTable, 
              con.type = "line", # ledges clutter the image visually here
              ratio=1, 
              linewidth = 0, 
              limits.expand = c(0.2, 0.1), # more space to the left and right of the plot
              label.buffer = unit(1, "mm"), # smaller buffer zone around clusters
              label.width = unit(25, "mm"), # wrap long lines
              label.margin = margin(1, 1, 1, 1, "pt"), # tighter label margins
              label.fontsize = 10) +
    theme_classic() + theme(legend.position = "none")
                               

## -----------------------------------------------------------------------------
sessionInfo()

