jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
193 stars 26 forks source link

Improve coloring options for `gt_merge_stack` #88

Closed francescoolivo closed 1 year ago

francescoolivo commented 1 year ago

Prework

This topic was never mentioned explicitely, but something similar can be found in #71, #53 and #39

Proposal

At the moment the column created using _gt_mergestack is hard to manipulate using the _taboptions function, it's possible to fill the cell with a color but I can't override the text color from the one passed in the function.

For instance, this works:

set.seed(12345)
dplyr::tibble(
    value = sample(state.name, 5),
    color_by = seq.int(10, 98, length.out = 5)
) %>%
    gt::gt() %>%
    tab_style(
        style = cell_fill(color = 'green'),
        locations = cells_body(
            columns = value,
            rows = color_by >= 80
        )
    )

But this does not work.

set.seed(12345)
dplyr::tibble(
    value = sample(state.name, 5),
    color_by = seq.int(10, 98, length.out = 5)
) %>%
    gt::gt() %>%
    gt_merge_stack(col1 = value, col2 = color_by) %>%
    tab_style(
        style = cell_text(color = 'green'),
        locations = cells_body(
            columns = value,
            rows = color_by >= 80
        )
    )

I think it would be a useful feature.

Also, I noticed that when you stack two floats you loose the decimal formatting on col2

jthomasmock commented 1 year ago

Hi @francescoolivo - thanks for the reprex! I'll ponder on this, but there's inherently a tradeoff of some of the functions such as gt_merge_stack(). We're taking raw text that gt knows how to handle and replacing it with raw HTML strings.

I don't expect that gt_merge_stack() will be able to be modified via tab_style() of the text itself, since there is no longer any raw text - it's raw HTML surrounding two individual text strings.

Here is one row before gt_merge_stack(), note the raw text in the table data cell element.

# raw table data cell element
<td headers="value" class="gt_row gt_left">Nevada</td>```
# gt_merge_stacked html
<td headers="value" class="gt_row gt_left">
  <div style="line-height:14px">
     <span style="font-weight:bold;font-variant:small-caps;color:#000000;font-size:14px">Pennsylvania</span>
  </div>
  <div style="line-height:10px">
    <span style="font-weight:bold;color:#BEBEBE;font-size:10px">76</span>
   </div>
</td>
francescoolivo commented 1 year ago

Makes sense. Maybe a less elegant option may be to pass the colors as columns containing a color, like in _scale_fill_identity`. For instance

set.seed(12345)
dplyr::tibble(
    value = sample(state.name, 5),
    color_by = seq.int(10, 98, length.out = 5)
) %>%
        mutate(color_col = case_when(color_by > 80 ~ 'green', TRUE ~ 'black')) %>%
    gt::gt() %>%
    gt_merge_stack(col1 = value, col2 = color_by, palette = c(color_col, 'black'))

Don't worry if it's a mess to implement, I can simply fill the whole cell, I realize that storing the color is not particularly elegant so it's not the best option to implement.

jthomasmock commented 1 year ago

Yah - part of this is that there are tradeoffs in one solution for another - at this point, the function is very focused on it's narrow use case. Thank you for the request though!