jthomasmock / gtExtras

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

gt_plt_bullet() function update #37

Closed Fabao1973 closed 2 years ago

Fabao1973 commented 2 years ago

I noticed that the target_color argument changed to target. As a result, it is no longer possible to apply the cols_label() function on the altered column.

I2

An alternative is to apply the keep_column = TRUE argument, but this keeps two columns, the data column and the graph column.

I1

I suggest reviewing this update, as in the previous version this was possible, and it is very useful.

jthomasmock commented 2 years ago

Howdy, can you provide the code you're using to generate the table?

Fabao1973 commented 2 years ago

Figure 1

I2

Data

f1_ful <- tidytuesdayR::tt_load(2021,'37')

Source data

data_results <- f1_full$results %>% clean_names()
data_constructors` <- f1_full$constructors %>% `clean_names()

Join tables

data_f1_trans <-
data_results %>%
select(result_id:position_order) %>%
left_join(data_constructors %>%
select(-url), by = 'constructor_id') %>%
mutate(type = case_when(position_order==1~'win',TRUE~'race')) %>%
group_by(name, type, race_id) %>%
summarise(result = n_distinct(name)) %>%
pivot_wider(names_from = 'type', values_from = 'result', values_fill = 0) %>%
summarise(race = sum(race), win = sum(win)) %>%
mutate(ratio = win/race, .after = 'win') %>%
filter(win>30) %>% arrange(desc(win))

Table

data_f1_trans %>%
gt() %>%
gt_plt_bullet(column = race,
target = win,
keep_column = FALSE,
colors = c('red','navy')) %>%
gt_theme_espn() %>%
fmt_symbol_first(column = ratio,
suffix = "%",
decimals = 1,
scale_by = 100) %>%
cols_label(
race = html("<span style='color:red'>Race</span> vs <span style='color:navy'>Win</span>")
) %>%
tab_header(title = "F1 races and wins",
subtitle = md("**Mercedes** leads wins to races ratio")) %>%
gt_highlight_rows(rows = name == "Mercedes",fill = "grey",alpha = 0.4) %>%
gt_add_divider(ratio,color = 'grey80',weight = px(1)) %>%
tab_source_note(md('**Data**: TidyTuesday **Package**: gtExtras by Thomas Mock'))

Figure 2

I1

Data

f1_full <- tidytuesdayR::tt_load(2021,'37')

Source data

data_results <- f1_full$results %>% clean_names()
data_constructors <- f1_full$constructors %>% clean_names()

Join tables

data_f1_trans <-
data_results %>%
select(result_id:position_order) %>%
left_join(data_constructors %>%
select(-url), by = 'constructor_id') %>%
mutate(type = case_when(position_order==1~'win',TRUE~'race')) %>%
group_by(name, type, race_id) %>%
summarise(result = n_distinct(name)) %>%
pivot_wider(names_from = 'type', values_from = 'result', values_fill = 0) %>%
summarise(race = sum(race), win = sum(win)) %>%
mutate(ratio = win/race, .after = 'win') %>%
filter(win>30) %>% arrange(desc(win))

Table

data_f1_trans %>%
gt() %>%
gt_plt_bullet(column = race,
target = win,
keep_column = TRUE,
colors = c('red','navy')) %>%
gt_theme_espn() %>%
fmt_symbol_first(column = ratio,
suffix = "%",
decimals = 1,
scale_by = 100) %>%
cols_label(
race = html("<span style='color:red'>Race</span> vs <span style='color:navy'>Win</span>")
) %>%
tab_header(title = "F1 races and wins",
subtitle = md("**Mercedes** leads wins to races ratio")) %>%
gt_highlight_rows(rows = name == "Mercedes",fill = "grey",alpha = 0.4) %>%
gt_add_divider(ratio,color = 'grey80',weight = px(1)) %>%
tab_source_note(md('**Data**: TidyTuesday **Package**: gtExtras by Thomas Mock'))
jthomasmock commented 2 years ago

Howdy!

Thanks for the issue report!

For future examples it's helpful to provide a minimal reprex, something small like below focused on the exact issue at hand:

library(gt)
library(gtExtras)

reprex_ex <- head(mtcars) %>% 
  dplyr::select(mpg:hp) %>% 
  gt() %>% 
  gt_plt_bullet(column = disp, target=hp)

Because we are merging the columns, one column name has to "win". Right now the function merges to the target column, but I can see how this could be confusing.

reprex_ex %>% 
  cols_label(disp = "disp2") %>% 
  gtsave("test.png")

To get your expected behavior, we can rename the target column.

reprex_ex %>% 
  cols_label(hp = "disp2") %>% 
  gtsave("test.png")

Created on 2022-03-04 by the reprex package (v2.0.1)

I appreciate the Issue note, and I'll likely alter this to reflect a change to merging to the column rather than target argument, which would prevent the behavior seen above.

jthomasmock commented 2 years ago

Reversed this behavior with latest version (0.2.4)

See reprex below:

library(gt)
library(gtExtras)

reprex_ex <- head(mtcars) %>% 
  dplyr::select(mpg:hp) %>% 
  gt() %>% 
  gt_plt_bullet(column = disp, target=hp)

reprex_ex %>% 
  cols_label(disp = "disp2") %>%
  gtsave("test.png")

Created on 2022-03-18 by the reprex package (v2.0.1)

Fabao1973 commented 2 years ago

Thank you so much!

Fabio Chiletto E-mail pessoal

Em sex., 18 de mar. de 2022 às 14:04, Thomas Mock @.***> escreveu:

Closed #37 https://github.com/jthomasmock/gtExtras/issues/37.

— Reply to this email directly, view it on GitHub https://github.com/jthomasmock/gtExtras/issues/37#event-6267595076, or unsubscribe https://github.com/notifications/unsubscribe-auth/AK5GOVFSPF33ZTEWDHKJ3G3VASZQBANCNFSM5OQBA7WQ . You are receiving this because you authored the thread.Message ID: @.***>