R 中具有指定范围的颜色 gt 表

发布于 2025-01-16 07:44:52 字数 305 浏览 0 评论 0原文

我想根据指定的值范围填充 gt 表的单元格。任何大于 0.95 的值都应为绿色,0.85-0.95 = 黄色,低于 0.85 的值应为红色。我尝试过 RColorBrewer 的 RdYlGn 调色板,但无法为其提供一系列值。

这是一些示例数据:

City         Jan   Feb   Mar  
Kansas City  .94  .90   .98
Chicago      .82  .87   .78
Detroit      .80  .92   .81
Miami        .98  1.00  .94

I'd like to fill the cells of a gt table based on a specified range of values. Any value greater than 0.95 = green, 0.85-0.95 = yellow, and below 0.85 should be red. I have tried RColorBrewer's RdYlGn palette but had trouble supplying a range of values to that.

Here is some sample data:

City         Jan   Feb   Mar  
Kansas City  .94  .90   .98
Chicago      .82  .87   .78
Detroit      .80  .92   .81
Miami        .98  1.00  .94

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我纯我任性 2025-01-23 07:44:52

gt 的 data_colorscales 包定义的调色板配合良好,因此您可以使用 col_bin 定义自己的分档调色板并将其传递给 <代码>数据颜色。有关比例尺颜色映射的更多详细信息/示例可以在此处找到:https://scales。 r-lib.org/reference/col_numeric.html

library(gt)
library(scales)

data <- data.frame(City = c('Kansas City','Chicago','Detroit','Miami'), 
                   Jan = c(.94, .82, .80, .98), 
                   Feb = c(.90, .87, .92, 1.0), 
                   Mar = c(.98, .78, .81, .94))

palette <- col_bin(palette=c('red','yellow','green'), bins=c(0,.85,.95,1.0))

gt(data) %>%
  data_color(columns=c('Jan','Feb','Mar'), colors=palette, apply_to='text') 

产生这些结果:

在此处输入图像描述

gt's data_color works well with color palettes defined by the scales package, so you can define your own binned color palette with col_bin and pass that to data_color. More details/examples on color mapping in scales can be found here: https://scales.r-lib.org/reference/col_numeric.html.

library(gt)
library(scales)

data <- data.frame(City = c('Kansas City','Chicago','Detroit','Miami'), 
                   Jan = c(.94, .82, .80, .98), 
                   Feb = c(.90, .87, .92, 1.0), 
                   Mar = c(.98, .78, .81, .94))

palette <- col_bin(palette=c('red','yellow','green'), bins=c(0,.85,.95,1.0))

gt(data) %>%
  data_color(columns=c('Jan','Feb','Mar'), colors=palette, apply_to='text') 

Producing these results:

enter image description here

怪我入戏太深 2025-01-23 07:44:52

像这样的事情:

我使用了橙色而不是黄色 ->更好地阅读,但您可以轻松更改。
不知何故,0.92 没有记录在序列中,所以我手动输入!

library(dplyr)
library(gt)
tbl1 <- df %>%
  gt()
nm1 <- names(df[,2:4])
 

for(i in seq_along(nm1)) {
  
  tbl1 <- tbl1 %>%
    tab_style(
      style = list(
        cell_text(color = "green", weight = "bold")
        #cell_fill(color = "red")
      ),
      locations = cells_body(
        columns = nm1[i],
        
        rows = tbl1

像这样的事情:

我使用了橙色而不是黄色 ->更好地阅读,但您可以轻松更改。
不知何故,0.92 没有记录在序列中,所以我手动输入!

_data`[[nm1[i]]] > 0.95 ) ) %>% tab_style( style = list( cell_text(color = "red", weight = "bold") # cell_fill(color = "blue") ), locations = cells_body( columns = nm1[i], rows = tbl1

像这样的事情:

我使用了橙色而不是黄色 ->更好地阅读,但您可以轻松更改。
不知何故,0.92 没有记录在序列中,所以我手动输入!

_data`[[nm1[i]]] < 0.85 ) ) %>% tab_style( style = list( cell_text(color = "orange", weight = "bold") # cell_fill(color = "blue") ), locations = cells_body( columns = nm1[i], rows = tbl1

像这样的事情:

我使用了橙色而不是黄色 ->更好地阅读,但您可以轻松更改。
不知何故,0.92 没有记录在序列中,所以我手动输入!

_data`[[nm1[i]]] %in% c(0.92, seq(0.85,0.95, 0.01)) ) ) } tbl1

输入图片此处描述

Something like this:

I used orange instead of yellow -> better to read but you can change easily.
Somehow 0.92 is not registered in the sequence so I put it by hand!

library(dplyr)
library(gt)
tbl1 <- df %>%
  gt()
nm1 <- names(df[,2:4])
 

for(i in seq_along(nm1)) {
  
  tbl1 <- tbl1 %>%
    tab_style(
      style = list(
        cell_text(color = "green", weight = "bold")
        #cell_fill(color = "red")
      ),
      locations = cells_body(
        columns = nm1[i],
        
        rows = tbl1

Something like this:

I used orange instead of yellow -> better to read but you can change easily.
Somehow 0.92 is not registered in the sequence so I put it by hand!

_data`[[nm1[i]]] > 0.95 ) ) %>% tab_style( style = list( cell_text(color = "red", weight = "bold") # cell_fill(color = "blue") ), locations = cells_body( columns = nm1[i], rows = tbl1

Something like this:

I used orange instead of yellow -> better to read but you can change easily.
Somehow 0.92 is not registered in the sequence so I put it by hand!

_data`[[nm1[i]]] < 0.85 ) ) %>% tab_style( style = list( cell_text(color = "orange", weight = "bold") # cell_fill(color = "blue") ), locations = cells_body( columns = nm1[i], rows = tbl1

Something like this:

I used orange instead of yellow -> better to read but you can change easily.
Somehow 0.92 is not registered in the sequence so I put it by hand!

_data`[[nm1[i]]] %in% c(0.92, seq(0.85,0.95, 0.01)) ) ) } tbl1

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文