knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(reactablefmtr) library(tidyverse) library(gapminder)
With {reactablefmtr}
, we can easily apply color tiles to columns within a table by using color_tiles()
within cell
of reactable::colDef()
.
By default a normalized orange-white-blue color scale is applied to each column.
df <- gapminder %>% filter(year == 2007) %>% select(-year) %>% relocate(lifeExp, .after = last_col()) df %>% reactable( defaultColDef = colDef( cell = color_tiles(.) ) )
color_tiles()
customization options| Parameter | Description | Default Value |
|:----------------------|:----------------------------------------------|:---------------------------------|
| data
| name of data set | NULL |
| colors
| color palette | c('#15607A','#FFFFFF','#FA8C00') |
| color_ref
| column containing color assignments | NULL |
| color_by
| column containing value assignments | NULL |
| opacity
| opaqueness of color palette | 1 |
| bias
| the spacing between colors | 1 |
| number_fmt
| the format of the values | NULL |
| text_size
| the size of the text | NULL |
| text_color
| the color of the text | 'black' |
| text_color_ref
| column containing text color assignments | NULL |
| show_text
| show or hide text | TRUE |
| brighten_text
| auto-adjust text color based on color of cell | TRUE |
| brighten_text_color
| color of the auto-adjusted text color | 'white' |
| bold_text
| bold format text | FALSE |
| span
| show as row-wise instead of column-wise | FALSE |
| box_shadow
| add a box shadow around tiles | FALSE |
| tooltip
| enable hover tooltip | FALSE |
| animation
| animation of color transitions on sort | 'background 1s ease' |
Numbers can be formatted within the number_fmt
argument in color_tiles()
. One method of formatting the numbers is by utilizing the formatters from the {scales}
package, and the numbers can be formatted in the same way as they are in {ggplot2}
.
df %>% reactable( columns = list( pop = colDef( cell = color_tiles(., number_fmt = scales::label_number_si(accuracy = 0.1)) ), gdpPercap = colDef( cell = color_tiles(., number_fmt = scales::comma) ), lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1)) ) ) )
If we want to show a different color palette than the default, we can call them within the colors
argument like so:
library(RColorBrewer) library(viridis) df %>% reactable( defaultSorted = 'pop', defaultSortOrder = 'desc', columns = list( pop = colDef( cell = color_tiles(., number_fmt = scales::label_number_si(accuracy = 0.1), colors = viridis::viridis(5)) ), gdpPercap = colDef( cell = color_tiles(., number_fmt = scales::comma, colors = RColorBrewer::brewer.pal(7, 'Greens')) ), lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1), colors = c('tomato','white','dodgerblue')) ) ) )
add_legend()
customization options| Parameter | Description | Default Value |
|:-------------|:-----------------------------------------|:---------------------------------|
| data
| name of dataset | NULL |
| col_name
| name of column containing numeric values | NULL |
| bins
| number of bins to be displayed | 5 |
| colors
| color palette | c('#15607A','#FFFFFF','#FA8C00') |
| bias
| opaqueness of color palette | 1 |
| labels
| show or hide value labels | TRUE |
| number_fmt
| the format of the values | NULL |
| title
| the title above the legend | NULL |
| footer
| the footer below the legend | NULL |
| align
| align to the left or right of the table | 'right' |
If you would like to add a legend for the color palette used within color_tiles()
, you can do so by including add_legend()
below the table and listing the color palette used within color_tiles()
. If no color palette is defined by the user within add_legend()
, it will show the default blue-to-orange color palette used in color_tiles()
.
df %>% reactable( columns = list( lifeExp = colDef( cell = color_tiles(., number_fmt = scales::comma) ) ) ) %>% add_legend(df, col_name = 'lifeExp')
You can add a title and a footer to the legend with title
and footer
respectively:
df %>% reactable( columns = list( lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1)) ) ) ) %>% add_legend(df, col_name = 'lifeExp', title = 'Life Expectancy (yrs)', footer = 'Reported as of 2007')
By default, the color palette is broken into 5 distinct bins. However, we can increase or decrease the number of color bins we would like to show in the legend by providing a number within bins
:
df %>% reactable( columns = list( lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1)) ) ) ) %>% add_legend(df, col_name = 'lifeExp', title = 'Life Expectancy (yrs)', footer = 'Reported as of 2007', bins = 9)
If you are using a different color palette than the default one provided, you can specify the color palette in the same way that you did within color_tiles()
:
df %>% reactable( columns = list( lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1), colors = viridis::viridis(5)) ) ) ) %>% add_legend(df, col_name = 'lifeExp', title = 'Life Expectancy (yrs)', footer = 'Reported as of 2007', colors = viridis::viridis(5))
If the data within your column is not evenly distributed, you can set the color bias to lean more towards the higher values or lower values within the column with bias
. Changing the bias within the legend is a good visual gauge of how the bias affects the distribution of colors within the column:
df %>% reactable( columns = list( lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1), colors = viridis::viridis(5), bias = 3) ) ) ) %>% add_legend(df, col_name = 'lifeExp', title = 'Life Expectancy (yrs)', footer = 'Reported as of 2007', colors = viridis::viridis(5), bias = 3)
Box shadows can be added to the tiles to create a '3-D' effect via box_shadow
.
df %>% reactable( columns = list( pop = colDef( cell = color_tiles(., number_fmt = scales::label_number_si(accuracy = 0.1), box_shadow = TRUE) ), gdpPercap = colDef( cell = color_tiles(., number_fmt = scales::comma, box_shadow = TRUE) ), lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1), box_shadow = TRUE) ) ) )
Colors can be conditionally assigned to values based on another column by using color_ref
.
In the example below, we assigned a blue color to Compact cars, a red color to Sporty cars, and a gold color to Vans using dplyr::case_when()
.
Then within color_tiles()
, we reference the name of the conditional column we just created to apply the colors to the values in MPG.city and MPG.highway.
df_continents <- df %>% mutate( continent_cols = dplyr::case_when( continent == 'Africa' ~ 'orange', continent == 'Americas' ~ 'pink', continent == 'Asia' ~ 'violet', continent == 'Europe' ~ 'gold', continent == 'Oceania' ~ 'skyblue', TRUE ~ 'grey' ) ) df_continents %>% reactable( defaultSorted = 'continent', defaultSortOrder = 'desc', columns = list( continent_cols = colDef(show = FALSE), pop = colDef( cell = color_tiles(., number_fmt = scales::label_number_si(accuracy = 0.1), color_ref = 'continent_cols') ), gdpPercap = colDef( cell = color_tiles(., number_fmt = scales::comma, color_ref = 'continent_cols') ), lifeExp = colDef( cell = color_tiles(., number_fmt = scales::label_number(accuracy = 1), color_ref = 'continent_cols') ) ) )
We can further apply the conditional colors to the entire dataset by setting the style within defaultColDef
:
df_continents %>% reactable( defaultSorted = 'continent', defaultSortOrder = 'desc', defaultColDef = colDef( cell = color_tiles(., color_ref = 'continent_cols') ), columns = list( continent_cols = colDef( show = FALSE ) ) )
The same conditional coloring can be applied based on numeric conditions as well. For example, if we wanted to highlight which cars have an MPG.city value of 23 or greater, we could use the same method as above but apply the conditions based on the MPG.city column instead of the Type column.
df_lifexp <- df %>% mutate( lifexp_cols = dplyr::case_when( lifeExp >= 80 ~ 'orange', TRUE ~ 'lightgrey' ) ) df_lifexp %>% reactable( defaultSorted = 'continent', defaultSortOrder = 'desc', defaultColDef = colDef( cell = color_tiles(., color_ref = 'lifexp_cols') ), columns = list( lifexp_cols = colDef( show = FALSE ) ) )
The color_by
argument allows for color assignment based on the values in another column.
df %>% reactable( columns = list( country = colDef( cell = color_tiles(., color_by = 'lifeExp') ) ) )
To help clarify what values the colors represent, you can add a legend below the table:
df %>% reactable( columns = list( country = colDef( cell = color_tiles(., color_by = 'lifeExp') ) ) ) %>% add_legend(df, col_name = 'lifeExp', title = 'Life Expectancy (yrs)', align = 'left')
By default, color_tiles()
conditionally assigns colors to values based on their relation to other values within that particular column. However, if the table you're showing is row-wise data, such as average temperatures by month for each year, then it will be difficult to compare how temperatures compare in each month:
By including span = TRUE
within our color_tiles()
formatter, we can conditionally assign colors to the values based on their relation to other values within the entire dataset, instead of within each column. Now our table displaying temperatures is much easier to read when comparing temperatures across months:
Note: the dataset for this example is sourced from the reactable demo cookbook
dimnames <- list(start(nottem)[1]:end(nottem)[1], month.abb) temps <- matrix(nottem, ncol = 12, byrow = TRUE, dimnames = dimnames) temps <- as_tibble(temps, rownames = 'Year') temppal <- c('#36a1d6', '#76b8de', '#a0bfd9', '#ffffff', '#d88359', '#d65440', '#c62c34') reactable( temps, defaultColDef = colDef( minWidth = 50, cell = color_tiles(temps, span = TRUE, colors = temppal) ) )
If we only wanted to apply color tiles to some of the months, we can do so by referencing either the numeric positions of the columns or the column names within span
:
reactable( temps, defaultColDef = colDef( minWidth = 50, cell = color_tiles(temps, span = 4:7, colors = temppal), ) )
Lastly, if you wanted to completely hide the text, you could do this by setting show_text = FALSE
, which displays the table as a heatmap as shown below:
Note: by setting tooltip = TRUE
, the values are still visible on hover.
population_data <- gapminder %>% filter(continent == 'Americas') %>% mutate(country = as.character(country), year = paste0("'", str_sub(year, 3, 4))) %>% select(country, year, lifeExp) %>% pivot_wider(names_from = year, values_from = lifeExp) reactable( population_data, compact = TRUE, pagination = FALSE, showSortIcon = FALSE, defaultSorted = "'52", defaultSortOrder = 'desc', defaultColDef = colDef( maxWidth = 50, cell = color_tiles(population_data, number_fmt = scales::comma, show_text = FALSE, tooltip = TRUE, span = TRUE) ), columns = list( country = colDef(maxWidth = 200) ) ) %>% add_title('Average Life Expectancy') %>% add_source('Data sourced from the {gapminder} package')
By default, the color of the text will auto-adjust based on the background color of the cell. To turn this feature off and set the text color to your own color, you can do so by setting brighten_text
to FALSE and then setting the text color within text_color
. You may also display the text in bold with bold_text
.
reactable( population_data, compact = TRUE, pagination = FALSE, showSortIcon = FALSE, defaultSorted = "'52", defaultSortOrder = 'desc', defaultColDef = colDef( maxWidth = 50, cell = color_tiles(population_data, number_fmt = scales::comma, brighten_text = FALSE, text_color = 'grey', bold_text = TRUE, span = TRUE) ), columns = list( country = colDef(maxWidth = 200) ) ) %>% add_title('Average Life Expectancy') %>% add_source('Data sourced from the {gapminder} package')
To change the size of the text, you can do so with text_size
.
reactable( population_data, compact = TRUE, pagination = FALSE, showSortIcon = FALSE, defaultSorted = "'52", defaultSortOrder = 'desc', defaultColDef = colDef( maxWidth = 50, cell = color_tiles(population_data, number_fmt = scales::comma, text_size = 24, span = TRUE) ), columns = list( country = colDef(maxWidth = 200) ) ) %>% add_title('Average Life Expectancy') %>% add_source('Data sourced from the {gapminder} package')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.