Yunuuuu / ggalign

A 'ggplot2' Extension for Consistent Axis Alignment
https://yunuuuu.github.io/ggalign/
Other
146 stars 7 forks source link

oncoplot #7

Closed Yunuuuu closed 2 weeks ago

Yunuuuu commented 2 months ago

A simple solution would be:

ggoncoplot <- function(data, ..., reorder_row = TRUE, reorder_column = TRUE) {
    data <- trimws(data, whitespace = "[\\h\\v]")
    data[data == ""] <- NA_character_
    counts <- !is.na(data)
    storage.mode(counts) <- "integer"
    if (reorder_column || reorder_row) {
        weights <- rowSums(counts)
        row_index <- order(weights, decreasing = TRUE)
    }
    ans <- ggheatmap(data = data, ...)
    if (reorder_row) {
        ans <- ans + hmanno("l") + align_order(row_index, reverse = TRUE)
    }
    if (reorder_column) {
        column_scores <- apply(vec_slice(counts, row_index), 2L, function(x) {
            score <- 2^(length(x) - seq_along(x))
            score[x == 0L] <- 0
            sum(score)
        })
        ans <- ans +
            hmanno("t") +
            align_order(order(column_scores, decreasing = TRUE))
    }
    ans + hmanno(NULL, plot_data = function(data) {
        data <- as.data.table(data)
        oths <- setdiff(names(data), "value")
        data$value <- strsplit(data$value, split = "[;,]", perl = TRUE)
        data <- data[, list(value = unlist(value, FALSE, FALSE)), by = oths] # nolint
        setDF(data)
    })
}
Yunuuuu commented 1 month ago
mat <- read.table(textConnection(
    "s1,s2,s3
g1,snv;indel,snv,indel
g2,,snv;indel,snv
g3,snv,,indel;snv"
), row.names = 1, header = TRUE, sep = ",", stringsAsFactors = FALSE)
mat <- as.matrix(mat)
ggoncoplot(mat)

image

Yunuuuu commented 2 weeks ago

close 23ed2681ed6eecb730581cf695a982bcfb00966f