【问题标题】:Is there a way to add a color scale legend in a gt table R?有没有办法在 gt 表 R 中添加色标图例?
【发布时间】:2023-02-15 08:22:04
【问题描述】:

我在四开本文档中有一个 gt() 表,其中的单元格根据它们的值进行了颜色填充。我想知道是否可以添加颜色图例比例,就像在 ggplot 中一样。

谢谢, 佩德罗

【问题讨论】:

  • 如果您提供一个最小的可重现示例,您将最大限度地提高获得有用答案的机会。 This post 可能有帮助。

标签: r gt


【解决方案1】:

您可以在需要图例的表格上方添加一个小的 GT 表格。

{r}
library(gt)
df <- data.frame(Lessthan5 = 'Less than 5', 
                 Equalto5 = 'Equal to 5', 
                 GreaterThan5 = 'Grater than 5') %>% 
  gt() %>% 
    tab_style(style = cell_fill(color = '#eeca87'), 
            locations = cells_body(columns=c(Lessthan5), 
                                   rows = Lessthan5 == 'Less than 5')) %>% 
  tab_style(style = cell_fill(color = '#c5c5c5'), 
            locations = cells_body(columns=c(Equalto5), 
                                   rows = Equalto5 == 'Equal to 5'))  %>% 
  tab_style(style = cell_fill(color = '#4EB89B'), 
            locations = cells_body(columns=c(GreaterThan5), 
                                   rows = GreaterThan5 == 'Grater than 5')) %>% 
  tab_options(column_labels.font.size = 0)

{r}
library(gt)

iris %>% 
  gt() %>% 
  tab_style(style = cell_fill(color = '#eeca87'), 
            locations = cells_body(columns=c(Sepal.Length), 
                                   rows = Sepal.Length < 5)) %>% 
  tab_style(style = cell_fill(color = '#c5c5c5'), 
            locations = cells_body(columns=c(Sepal.Length), 
                                   rows = Sepal.Length == 5))  %>% 
  tab_style(style = cell_fill(color = '#4EB89B'), 
            locations = cells_body(columns=c(Sepal.Length), 
                                   rows = Sepal.Length > 5))

【讨论】:

    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多