如何在data.frame中按值对单元格进行着色?

时间:2017-02-21 20:58:48

标签: r dataframe colors

我有一个名为“fdr”的data.frame。它看起来像这样:

         pi     pd      aa      ef   
gene1   0.78   0.04   0.89    0.01
gene2   0.06   0.95   0.02    0.03
gene3   0.98   0.07   0.03    0.23

现在我想要为红色小于0.05的所有细胞着色。 我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

对于将来的问题,请打印dput(fdr)的结果。这将使其他人更容易回答。

考虑使用formattable package

formattable(df, list(
   pi = formatter("span", style = x ~ ifelse(x < 0.05, style(color = "red", font.weight = "bold"), NA)),
   pd = formatter("span", style = x ~ ifelse(x < 0.05, style(color = "red", font.weight = "bold"), NA)),
   aa = formatter("span", style = x ~ ifelse(x < 0.05, style(color = "red", font.weight = "bold"), NA)),
   ef = formatter("span", style = x ~ ifelse(x < 0.05, style(color = "red", font.weight = "bold"), NA))
))

结果如下:
     enter image description here

这并不能完全达到你想要的效果(就细胞着色而言)。但似乎你试图强调特定情况(<0.05),这肯定抓住了这一点。