0

The code below creates a small table. It is unclear to me how to make these changes:

1) Less white space between the two bars. Using Inspect Element I can change the padding from 8px to 3px in .table>tfoot>tr>td. Is that the right approach? If so, how do I add the appropriate css to my R Script?

2) Remove the rounding of the color bars. Again, inspect element shows that if I change border_radius:0px and padding-right:0px for each cell the change occurs. But again, this doesn't seem correct.

3) How do I change the color of the font of the text in the cells that have the bars?


library(formattable)
library(kableExtra)
library(knitr)

fraction <- function(x, df) {
  x/df$count
}

df <- tibble (
  Type = c("A", "B", "C"),
  count = c(500, 350, 860),
  Decreasing = c(226, 103, 507),
  Increasing = c(300, 250, 350)
) 


mutate(df,
       Decreasing = color_bar(color = "lightgrey", fun = "fraction", df)(Decreasing),
       Increasing = color_bar(color = "lightgreen", fun = "fraction", df)(Increasing)
) %>% 
  select(Type, Decreasing, Increasing) %>% 

  kable("html", escape = "F", align = c("l", "r", "l")) %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "responsive"), full_width = F, position = "float_left")
ixodid
  • 2,180
  • 1
  • 19
  • 46

2 Answers2

3

color_bar is from the formattable package but the good news is that you can define your own color_bar function (just type color_bar in your R console will give you the source code of color_bar and then you can modify it). It will solve your question 2 & 3.

color_bar2 <- function (color = "lightgray", fun = "proportion", ...) 
{
  fun <- match.fun(fun)
  formatter(
    "span", 
    style = function(x) style(
      display = "inline-block", 
      direction = "rtl", `border-radius` = "0px", `padding-right` = "2px", 
      `background-color` = csscolor(color), color = csscolor("red"),
      width = percent(fun(as.numeric(x), ...))))
}

mutate(df,
       Decreasing = color_bar2(color = "lightgrey", fun = "fraction", df)(Decreasing),
       Increasing = color_bar2(color = "lightgreen", fun = "fraction", df)(Increasing)
) %>% 
  select(Type, Decreasing, Increasing) %>% 

  kable("html", escape = "F", align = c("l", "r", "l")) %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "responsive"), full_width = F, position = "float_left")

For question 1, if you are rendering your table in rmarkdown, check out this page https://rmarkdown.rstudio.com/html_document_format.html#custom_css for how to use custom_css in rmarkdown.

Hao
  • 7,476
  • 1
  • 38
  • 59
0

If you don't mind a different package (my own):

library(tibble)
library(dplyr)
library(huxtable)
df <- tibble (
  Type = c("A", "B", "C"),
  count = c(500, 350, 860),
  Decreasing = c(226, 103, 507),
  Increasing = c(300, 250, 350)
) 

ht <- as_hux(df) 
ht %>% 
       select(Type, Decreasing, Increasing) %>% 
       set_all_padding('3px') %>% 
       set_text_color(everywhere, 2:3, "red") %>% 
       add_colnames()

That doesn't get you the colour bars though. (Hmm, perhaps I should add a function.) You can have those by... something like:

my_color_bar <- color_bar(color = "lightgrey", fun = "fraction", df)

ht %>% 
  select(Type, Decreasing, Increasing) %>% 
  set_all_padding('3px') %>% 
  set_text_color(everywhere, 2:3, "red") %>% 
  set_escape_contents(everywhere, 2:3, TRUE) %>% 
  set_number_format(everywhere, 2:3, list(my_color_bar)) %>% 
  add_colnames()