This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R, plot text table

I am trying to plot a text table like below in R but with no luck. Any idea how this could be done? I want table with highlighted cells according to text Example, Cell highlight in green if text is a2, and blue if text is b2, half green and half yellow if text is a2/f1. In the following example ID is the sample ID and Column name is the name of the segment of the genome. Column values are the segment designation. Here is an example of what I am aiming at (Figure 5 of the manuscript) https://www.tandfonline.com/doi/pdf/10.1038/emi.2017.60.

table1

ID AA AB AC AD AE
1   a2  a2  b1  c1   a2
2   b1  a2  a2/f1 c1 a2
3   b1  b1  a2/f1 c1 a2

data<-read.table("mydata", header=TRUE)

do not know how to go any further. Ill appreciate any help.

r plot

Please expand on how this question is related to bioinformatics. If this is not bioinformatics, please ask Stack Overflow.

You're looking for something like Excel's Conditional Formatting. R is a data processing language, unless you use visualization libraries (markdown or HTML libraries), adding colors to text is not something R is particularly useful for. You might benefit from using HTML and javascript/jQuery to get this done.

Added some information about the example data.

This question is not about bioinformatics...

Hello akang!

We believe that this post does not fit the main topic of this site.

Not a bioinformatics question.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

Since it is a biological dataset I think Biostars community suits it the best. This particular problem aims at visualizing a viral gene segment from different samples and understand its origin. There are all sort of biological datasets and this might be a new one for the community. Here is an example of what I am aiming at (Figure 5 of the manuscript) https://www.tandfonline.com/doi/pdf/10.1038/emi.2017.60.

Please edit your question and mention the paper and the figure so it is evident you're dealing with biological data. I'll reopen your question now.

2 answers

Like RamRS is already telling you, it is not something R is useful for. However, it is possible with package crayon. The following example is taken from this blog.

library(crayon)

cat(green(
  'I am a green line ' %+%
  blue$underline$bold('with a blue substring') %+%
  yellow$italic(' that becomes yellow and italicised!\n')
))

It works in the linux R console, but not on windows.

This colors the text, not the "cell" the text is contained. That is because coloring cells for display is not something R does.

Yes the example colors text, but in the manual you can also find how to color background.

You're going to need to reshape2::melt() your dataset and use ggplot2 (geom_tile, scale_color_manual and most probably legend guides) to get to that figure.

Try a basic geom_tile with fill=<column_name> before adding on the scale_color_manual.

Log in to answer this question.