【问题标题】:Ho to highlight conditional values in a two way table graphing with TableGrob in R?如何在 R 中使用 TableGrob 以双向表格图形方式突出显示条件值?
【发布时间】:2021-10-04 04:14:17
【问题描述】:

例如,我想在 R 中绘制一个表格,在该表格中突出显示条件为“小于 2”的红色值。关于如何做到这一点的任何帮助?

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)

【问题讨论】:

  • 欢迎来到 SO!是强制使用gridgridExtra 包还是你可以使用其他包?
  • 我可以接受所有的包,没有具体要求。谢谢:)

标签: r conditional-statements gridextra grob


【解决方案1】:

您可以使用gridExtracondformat 来简化:

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)

【讨论】:

  • @CamillaM 有帮助吗?
  • 嗨@s__ 它看起来很棒!但现在我看到使用这个包我无法将最后的图表与“ggarrange”结合起来。你认为有可能使用与 ggplot2 兼容的其他包来获得这样的图表吗?干杯
  • @CamillaM 我明白了,kableExtra 存在一些问题。这里使用gridExtra 的解决方案似乎比我想象的更强制性。
  • 我成功地做了我的图表,非常感谢@s__,你太棒了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 2020-05-26
  • 2019-06-13
  • 2014-04-04
  • 2017-12-19
  • 2020-02-24
相关资源
最近更新 更多