简体   繁体   English

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

[英]Visualize frequency of categorical variables between different samples?

Hi I would like to generate a plot that looks like this. 嗨,我想生成一个像这样的情节。 在此处输入图片说明

At first glance this looks like an upset plot however it is not because I don't want to tabulate the intersection. 乍一看,这看起来像是一个烦恼的图,但这不是因为我不想对交点进行制表。 I have the code below to generate a plot that tabulates intersection in upset plot but what I want is to just count the total and show the intersection like the above image. 我有下面的代码来生成一个图表,该图表将心烦图中的交点制成表格,但是我想要的是只计算总数并像上图所示显示交点。 Is this possible? 这可能吗?

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)

It's doable in base plot but maybe not worth the trouble. 它在基本情节中是可行的,但也许不值得这样做。

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM