繁体   English   中英

barplot()基于列中类别数的条形图的不同颜色的灰色

[英]barplot() different colors of grey for bars based on number of categories in a column

我在R中有一个数据帧。在每个数据帧中有8个变量。

我想制作一个条形图,我想根据一个特定变量中的值为条形指定不同的灰度。

这就是我制作一个条形图的方式:

barplot(x$cov, names.arg = x$exon, 
    xlab = x$gene[1],
    ylab = "read depth" , 
    border = gray.colors(???))

变量外显子由整数组成。 例如:

0 0 0 0 1 1 1 2 2 3 3 3 3 4 4 4

所以我想要$exon = 0的所有条形都是一个灰色阴影所有的条形图,其中$exon = 1 ,另一个灰色等等......

有人可以帮我一把吗?

首先,定义包含covexon值的样本数据框。

x<-data.frame(cov=sample(1:20,20),exon=rep(0:4,each=4))

使用参数col=而不是border=因为它在示例中更好地可见。

灰色颜色传染媒介被做作为exon的独特的价值的长度。 然后[]用于根据exon值为每个条形选择颜色。

barplot(x$cov, names.arg = x$exon, 
        ylab = "read depth" , 
        col = gray.colors(length(unique(x$exon)))[as.factor(x$exon)])

在此输入图像描述

暂无
暂无

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

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