How to Use More Than 2 Colors in the Color_Tile Function

How to combine formatter and color_tile functions in a table using formattable

Try to define your own formatter with the formatter function and combine background color with font color.

formatter

The color_tile effect is defined by display, pading, border-radius and background-color. And color sets the rules for font colors.

costum_format <- formatter(.tag = "span", style = function(x) style(
display = "block",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = csscolor(gradient(as.numeric(x),
customGreen, customGreen0)),
color = ifelse(x >= 3, "red","black")))

use costum function

formattable(mydata, 
align =c("l","c","c"),
list(
`col2` = costum_format))

output

Sample Image

R Formattable apply two format to an area

I had the same problem a while back and had to create my own formatter. Here's the formatter with code used to create a table similar to yours. Just adjust the tags inside the style.

library(tidyverse)
library(formattable)

colorbar <- function(color = "lightgray", fun = "comma", digits = 0) {
fun <- match.fun(fun)
formatter("span", x ~ fun(x, digits = digits),
style = function(y) style(
display = "inline-block",
direction = "rtl",
"border-radius" = "4px",
"padding-right" = "2px",
"background-color" = csscolor(color),
width = percent(proportion(as.numeric(y), na.rm = TRUE))
)
)
}

set.seed(123)
df <- data.frame(id = as.factor(1:10),
a = rnorm(10), b = rnorm(10), c = rnorm(10)) %>%
mutate_if(is.numeric, percent)

tbl <- df %>%
formattable(list(area(col = 2:4) ~ colorbar(color = "pink", fun = "percent", digits = 2))) %>%
as.htmlwidget()

If you have questions, let me know!

conditional color format every second row in a column with formattable (or any other package)

Like this?

df = data.frame(Group = c('1.a', '1.a', '2.b', '2.b', '3.c', '3.c'),
Obesity = c('1(ref)', 35, '0.6 (0.5, 0.7)', 25, '0.7 (0.6, 0.9)', 20))
library(formattable)
formattable(df, list(area(row = seq(2, nrow(df), by=2), col = Obesity) ~
color_tile("transparent", "pink")))

Sample Image

PS: indeed, the question could be asked in a clearer way ;-)

Coloring Formattable Data Table in R - Stock Data

A possible solution will be to create multiple ifelse statement in order to set 5 different colors (red / lightred / white / lightgreen / green) in function of the value of each columns.

Here, a possible way of doing it:

Test <- formatter("span",
style = x ~ style(display = "block", font.weight = "bold",color = "black","border-radius" = "4px",
"padding-right" = "4px",
"background-color" = ifelse(x <= -10, "red", ifelse(x > -10 & x <= -2, "tomato", ifelse(x > -2 & x <= 2, "white", ifelse(x > 2 & x <= 10, "palegreen",ifelse(x > 10, "green",NA)))))),
x ~ percent(x/100))

formattable(stocks_df,align =c("l","c","c","c","c"), list(
`Stock` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")),
day = Test,
week = Test,
month = Test))

Sample Image

Does it answer your question ?

Reproducible example

structure(list(Stock = c("AAPL", "AMZN", "BA", "PYPL"), day = c(1.5, 
3.2, -2.1, -5), week = c(3.2, 5.3, -4, -8.5), month = c(10.6,
4.4, -10.5, -12.1)), row.names = c(NA, -4L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x55b7eeb735c0>)

In formattable, can the background colour be determined by a different column

Use the code below to set up the backgroup colours you like the most for col3. For now, I left the one you chose.

formattable(df,
list(col1 = formatter("span", style = ~ style(color = "black",font.weight = "bold")),
col2 = color_tile("lightseagreen", "lightskyblue"),
col3 = formatter("span",
style = ~style(display = "block",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = ifelse(col4 == 1, "red",
ifelse(col4 == 0, "green",
ifelse(col4 == -1, "yellow", "blue")))))))

Sample Image


I found out that this was possible by looking at the code of the function color_tile:

> color_tile
function (...)
{
formatter("span", style = function(x) style(display = "block",
padding = "0 4px", `border-radius` = "4px",
`background-color` = csscolor(gradient(as.numeric(x),
...))))
}
<bytecode: 0x00000225fccd6e08>
<environment: namespace:formattable>

Change color_bar color based on odd or even rows, R table

We may use the row/col in area

formattable(i1, align =c("l","c","c","c","r", "c", "l", "l", "r"), list(
`Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")),
`2011`= color_tile(customGreen, customGreen0),
`2012`= color_tile(customGreen, customGreen0),
`2013`= color_tile(customGreen, customGreen0),
`2014`= color_bar(customRed),
`2015`= color_tile(customGreen, customGreen0),
`2016`= color_bar(customGreen),
area(row = c(1, 3), col = `Average`) ~ color_bar3(customRed),
area(row = c(2, 4), col = `Average`) ~ color_bar3("lightblue")
))

-output

Sample Image



Related Topics



Leave a reply



Submit