data_color() can be used without any supplied arguments to colorize a gt table. Let’s do this with the exibble dataset:
exibble |>gt() |>data_color()
num
char
fctr
date
time
datetime
currency
row
group
1.111e-01
apricot
one
2015-01-15
13:35
2018-01-01 02:22
49.950
row_1
grp_a
2.222e+00
banana
two
2015-02-15
14:40
2018-02-02 14:33
17.950
row_2
grp_a
3.333e+01
coconut
three
2015-03-15
15:45
2018-03-03 03:44
1.390
row_3
grp_a
4.444e+02
durian
four
2015-04-15
16:50
2018-04-04 15:55
65100.000
row_4
grp_a
5.550e+03
NA
five
2015-05-15
17:55
2018-05-05 04:00
1325.810
row_5
grp_b
NA
fig
six
2015-06-15
NA
2018-06-06 16:11
13.255
row_6
grp_b
7.770e+05
grapefruit
seven
NA
19:10
2018-07-07 05:22
NA
row_7
grp_b
8.880e+06
honeydew
eight
2015-08-15
20:20
NA
0.440
row_8
grp_b
What’s happened is that data_color() applies background colors to all cells of every column with the default palette in R (accessed through palette()). The default method for applying color is "auto", where numeric values will use the "numeric" method and character or factor values will use the "factor" method. The text color undergoes an automatic modification that maximizes contrast (since autocolor_text is TRUE by default).
You can use any of the available method keywords and gt will only apply color to the compatible values. Let’s use the "numeric" method and supply palette values of "red" and "green".
With those options in place we see that only the numeric columns num and currency received color treatments. Moreover, the palette colors were mapped to the lower and upper limits of the data in each column; interpolated colors were used for the values in between the numeric limits of the two columns.
We can constrain the cells to which coloring will be applied with the columns and rows arguments. Further to this, we can manually set the limits of the data with the domain argument (which is preferable in most cases). Here, the domain will be set as domain = c(0, 50).
We can use any of the palettes available in the RColorBrewer and viridis packages. Let’s make a new gt table from a subset of the countrypops dataset. Then, through data_color(), we’ll apply coloring to the population column with the "numeric" method, use a domain between 2.5 and 3.4 million, and specify palette = "viridis".
We can alternatively use the fn argument for supplying the scales-based function scales::col_numeric(). That function call will itself return a function (which is what the fn argument actually requires) that takes a vector of numeric values and returns color values. Here is an alternate version of the code that returns the same table as in the previous example.
Using your own function in fn can be very useful if you want to make use of specialized arguments in the scales::col_*() functions. You could even supply your own specialized function for performing complex colorizing treatments!
data_color() has a way to apply colorization indirectly to other columns. That is, you can apply colors to a column different from the one used to generate those specific colors. The trick is to use the target_columns argument. Let’s do this with a more complete countrypops-based table example.
When specifying a single column in columns we can use as many target_columns values as we want. Let’s make another countrypops-based table where we map the generated colors from the year column to all columns in the table. This time, the palette used is "inferno" (also from the viridis package).
Now, it’s time to use pizzaplace to create a gt table. The color palette to be used is the "ggsci::red_material" one (it’s in the ggsci R package but also obtainable from the paletteer package). Colorization will be applied to the to the sold and income columns. We don’t have to specify those in columns because those are the only columns in the table. Also, the domain is not set here. We’ll use the bounds of the available data in each column.
Colorization can occur in a row-wise manner. The key to making that happen is by using direction = "row". Let’s use the sza dataset to make a gt table. Then, color will be applied to values across each ‘month’ of data in that table. This is useful when not setting a domain as the bounds of each row will be captured, coloring each cell with values relative to the range. The palette is "PuOr" from the RColorBrewer package (only the name here is required).
Notice that na_color = "white" was used, and this avoids the appearance of gray cells for the missing values (we also removed the "NA" text with sub_missing(), opting for empty strings).