简体   繁体   English

kableextra 表列中的背景颜色

[英]Background color in kableextra table column

I have simple RMarkdown document exporting in pdf containing a kablextra table with only 2 columns (see image).我在 pdf 中导出了简单的 RMarkdown 文档,其中包含一个只有 2 列的 kablextra 表(见图)。 I'm trying to set the background of both columns to a shade of blue where high values are represented with a darker color and low values with a lighter color.我正在尝试将两列的背景设置为蓝色阴影,其中高值用较暗的颜色表示,低值用较浅的颜色表示。 The order of the values cannot change as they represent monthly figures.值的顺序不能改变,因为它们代表月度数字。 As I was experimenting to find a solution, I managed to get the columns to have some background color (see image).当我尝试寻找解决方案时,我设法让列具有一些背景颜色(见图)。

library(kableExtra)
library(paletteer)
library(tidyverse)

#two vectors

column_1<-c(144, 189, 213, 231, 229, 235, 216, 221, 221, 200, 204, 236)
column_2<-c(83.7, 92.5, 87.6, 88.2, 80.5, 72.6, 66.7, 71.9, 66.7, 58.2, 72.1, 72.7)


#create dataframe
activity<-data.frame (column_1, column_2)

#create kableextra summary table 

kbl(activity, booktabs = T, linesep = "",  col.names = linebreak(c("Column 1", "Column 2")), align = "c", caption = "Summary Table") %>%
  kable_styling(full_width = F) %>%
  kable_styling(font_size = 12, position = "center") %>% 
  kable_styling(latex_options = "hold_position") %>% 
  column_spec(1, color = "black", background = paletteer_d("ggsci::blue_material")) %>%
  column_spec(2, color = "white", background = spec_color(activity$column_1, end = 0.9, option = "viridis", direction = -1)) 
  

在此处输入图像描述

The left column is using the following code and is exactly the color I want but I can't get it to highlight values properly (high with dark and low with light color - as mentioned above I can't sort the low to high as they are timeseries data):左列使用以下代码,正是我想要的颜色,但我无法正确突出显示值(高为深色,低为浅色 - 如上所述,我无法将低到高排序,因为它们是时间序列数据):

column_spec(1, color = "black", background = paletteer_d("ggsci::blue_material")) %>%

The right column is highlighting high & low values exactly how I want it to but is not using the color I want!右列突出显示了我想要的高值和低值,但没有使用我想要的颜色! (people at work don't like it actually!) (工作中的人实际上不喜欢它!)

column_spec(2, color = "white", background = spec_color(activity$column_2, end = 0.9, option = "viridis", direction = -1)) %>%

Is there an argument that can be added in the code of the left column to make it highlight high values with darker color etc like the right column does but without sorting the values in the column?是否可以在左列的代码中添加一个参数,使其像右列一样突出显示颜色较深的高值等,但不对列中的值进行排序?

Thanks for taking the time to read my post感谢您花时间阅读我的帖子

Regards问候

Dim暗淡

The spec_color function you are using doesn't give an option for a different palette, but it's pretty simple:您正在使用的spec_color function 没有提供不同调色板的选项,但它非常简单:

> spec_color
function(x, alpha = 1, begin = 0, end = 1,
                       direction = 1, option = "D",
                       na_color = "#BBBBBB", scale_from = NULL) {
  if (is.null(scale_from)) {
    x <- round(rescale(x, c(1, 256)))
  } else {
    x <- round(rescale(x, to = c(1, 256),
                       from = scale_from))
  }

  color_code <- viridisLite::viridis(256, alpha, begin, end, direction, option)[x]
  color_code[is.na(color_code)] <- na_color
  return(color_code)
}
<bytecode: 0x7fb1aae1a0a8>
<environment: namespace:kableExtra>

You could write your own alternative that uses your preferred palette:您可以编写自己的替代方案,使用您喜欢的调色板:

spec_color2 <- function(x, alpha = 1, begin = 0, end = 1,
         direction = 1, option = "D",
         na_color = "#BBBBBB", scale_from = NULL,
         palette = viridisLite::viridis(256, alpha, begin, end, direction, option)) {
  n <- length(palette)
  if (is.null(scale_from)) {
    x <- round(scales::rescale(x, c(1, n)))
  } else {
    x <- round(scales::rescale(x, to = c(1, n),
                       from = scale_from))
  }
  
  color_code <- palette[x]
  color_code[is.na(color_code)] <- na_color
  return(color_code)
}

Then your table would be displayed using this code:然后您的表格将使用以下代码显示:

kbl(activity, booktabs = T, linesep = "",  col.names = linebreak(c("Column 1", "Column 2")), align = "c", caption = "Summary Table") %>%
  kable_styling(full_width = F) %>%
  kable_styling(font_size = 12, position = "center") %>% 
  kable_styling(latex_options = "hold_position") %>% 
  column_spec(1, color = "black", background = spec_color2(activity$column_1, palette = paletteer_d("ggsci::blue_material"))) %>%
  column_spec(2, color = "white", background = spec_color2(activity$column_2, palette = paletteer_d("ggsci::blue_material"))) 

截屏

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

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