可视化不同样本之间的分类变量的频率?

时间:2019-03-22 19:24:52

标签: r ggplot2 tidyverse

嗨,我想生成一个看起来像这样的情节。 enter image description here

乍一看,这看起来像是一个沮丧的情节,但这不是因为我不想对交点进行制表。我有下面的代码来生成一个图表,该图表将心烦图中的交点制成表格,但是我想要的是只计算总数并像上面的图像一样显示交点。这可能吗?

library ( ComplexHeatmap)
lt = list(drug1 = c ( "s1","s2","s3"),
          drug2 = c ( "s1","s2"),
          drug3 = c ( "s2")  )

m1 = make_comb_mat(lt) # the default mode is `distinct`

length ( unique ( unlist ( lt) ) ) # three samples

UpSet( m1)

1 个答案:

答案 0 :(得分:1)

这在基本情节中是可行的,但也许不值得麻烦。

lt = list(drug1 = c ( "s1","s2","s3"),
          drug2 = c ( "s1","s2"),
          drug3 = c ( "s2"))

bc = table(unlist(lt))
bc = bc[order(names(bc))]

m = sapply(sort(unique(unlist(lt))), function(x) sapply(lt, function(y) x %in% y))

margin = 0.25

graphics.off()
plot(1, 1,
     xlim = c(1, length(bc)),
     ylim = c(0, max(bc) + NROW(m) + margin),
     type = "n",
     ann = FALSE, 
     axes = FALSE)
#box()

for (i in seq_along(bc)){
    lines(rbind(c(i, 0), c(i, bc[i])), lwd = 8, lend = "butt")
}

for (i in 1:NROW(m)){
    for (j in seq_along(bc)){
        if (m[i, j]){
            points(j, i + max(bc) + margin, cex = 2, lwd = 2)
            if (i > 1 & m[max(1, i-1), j]){
                lines(rbind(c(j, i - 1 + max(bc) + margin),
                            c(j, i + max(bc) + margin)))
            }
        }
    }
}

axis(1, at = 1:NCOL(m), labels = colnames(m), las = 1)
# axis(2, at = seq_along(bc), labels = seq_along(bc), las = 1)
axis(2, at = (1:NROW(m))+ max(bc) + margin, labels = row.names(m), las = 1)

enter image description here