简体   繁体   English

将颜色与 gt 中的列值匹配

[英]Matching colours with column values in gt

I have a simple piece of code that tries to publish a table which colours columns according to the colour listed in the contents using the package gt.我有一段简单的代码试图发布一个表格,该表格使用 package gt 根据内容中列出的颜色为列着色。

df <- data.frame(name = c("john", "paul", "ringo"),
                 fav_colour = c("red", "blue", "green"))

df %>% gt() %>% data_color(columns = fav_colour, colors = fav_colour)

However, as you can see the colours plotted do not match up correctly.但是,如您所见,绘制的颜色没有正确匹配。

I appreciate that this is a known issue in gt, as described on package github and that no fix has been implemented to the package yet.我知道这是 gt 中的一个已知问题,如package github中所述,并且尚未对 package 实施任何修复。

I was wondering if either someone has discovered a workaround to this in gt or can recommend a different table publishing package that has equivalent functionality.我想知道是否有人在 gt 中发现了解决此问题的方法,或者可以推荐具有等效功能的不同表发布 package。

Try this...尝试这个...

library(tidyverse)
library(gt)

df <- data.frame(name = c("john", "paul", "ringo"),
                 fav_colour = c("red", "blue", "green"))

df %>% 
  mutate(fav_colour = fct_inorder(fav_colour)) |> 
  gt() %>% 
  data_color(columns = fav_colour, colors = as.character(fav_colour))

在此处输入图像描述

Created on 2022-04-28 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-04-28

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM