0

I have a data.frame called "fdr". It looks like this:

         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

Now I want to color all cells which are smaller than 0.05 in red. How can I do that?

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 5
    You're going to need to expand on this rather a lot. "Coloring cells in a data frame" is sort of nonsensical concept in R, unless you are talking about displaying the data frame in an HTML table, or exporting it to some other specific format that supports a spreadsheet like interface. And in those cases, you'll need to explain how exactly you intend to view the data, what packages you're using, etc. – joran Feb 21 '17 at 21:03
  • I use the grid.table function and the heatmap.2 function. – Melanie Julia Feb 21 '17 at 21:11
  • Ok, then in addition to the option below, consider [this](http://stackoverflow.com/q/18663159/324364) question, maybe? Or [this](http://stackoverflow.com/q/23819209/324364), or [this](http://stackoverflow.com/q/18414001/324364)? – joran Feb 21 '17 at 21:13

1 Answers1

3

For future questions, please print the result of dput(fdr). This will make it a bit easier for others to answer.

Consider using the 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))
))

Result looks like:
enter image description here

This doesn't fully achieve what you want (in terms of coloring the cells). But it seems that you were trying to highlight specific cases (<0.05) and this definitely captures that.

msubbaiah
  • 350
  • 2
  • 14
  • Thank you! But there is an error in formatC(c(0.42, 0.43, 0.05, 0, 0, 0, 0.56, 0.11, 0.03, 0.55, : (list) object can not be changed to integer – Melanie Julia Feb 22 '17 at 09:30