1

I would like to graph a table in R where I highlight a value with a condition of 'less than 2' in Red, for example. Any help on how I can do this?

data(iris)
iris <- iris[1:4, 1:3]
rownames(iris) <- as.character(as.yearmon(
  seq(as.Date("2020/1/1"), as.Date("2020/4/1"), by = "month")))
colnames(iris) <- as.character(as.yearmon(
  seq(as.Date("2020/5/1"), as.Date("2020/8/1"), by = "month")))
iris

tg <- tableGrob(iris)
grid.draw(tg)
s__
  • 9,270
  • 3
  • 27
  • 45
CamillaM
  • 23
  • 5

1 Answers1

0

You can use gridExtra and condformat to make it easier:

library(gridExtra)
library(condformat)

t <- condformat(iris) %>%
     rule_text_color(Sepal.Length, ifelse(Sepal.Length >= 4.6, "red", "")) %>%
     condformat2grob()

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
     geom_point()

grid.arrange(p ,t, nrow=1)

enter image description here

s__
  • 9,270
  • 3
  • 27
  • 45
  • @CamillaM does it help? – s__ Jul 29 '21 at 22:04
  • Hi @s__ it looks great! But now I see that using this package I cannot combine graphs at the end with 'ggarrange'. Do you think it would be possible to get such a graph using other package that is compatible with ggplot2? Cheers – CamillaM Jul 30 '21 at 13:01
  • @CamillaM I see, with `kableExtra` there is some problem to put it in. Here a solution with `gridExtra`, seems to be more compulsory than I've thought. – s__ Jul 30 '21 at 14:24
  • I managed to do my graphs, thanks a lot @s__, you are awesome :) – CamillaM Aug 02 '21 at 12:45