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_bar_stack only produce one group #18

Closed rizqrama closed 2 years ago

rizqrama commented 2 years ago

Hi! I tried to make a stacked barchart as a part of gt table. However, when I run the code, the table didn't visualize/produce the bar of the second group.

Here's a little bit detail of my data

Screenshot 2021-10-15 104816

and this is the code I wrote

clr_cmp <- c("#1976D2", "orange")

pov_linec %>% 
  gt() %>% 
  gt_plt_bar_stack(
    column = pov_linec,
    position = "stack",
    labels = c("Kab/Kota", "Provinsi"),
    palette = clr_cmp,
    width = 60
  )

However, this is the result I got:

Screenshot 2021-10-15 104849

and this is what I have tried:

unfortunately, none of those work.

Thank you for hearing me out! Love to use gtExtras for my further research

jthomasmock commented 2 years ago

Thanks for reporting! There was an error in the package (selecting a 3rd color when there are only 2). I'll push an update shortly, but am adding a test to protect against this as well.

jthomasmock commented 2 years ago

For your issues, here's a quick reprex:

library(gtExtras)
library(gt)

packageVersion("gtExtras")
#> [1] '0.2.19'

generate_df(
  n = 2, n_grps = 10, mean = rep(400000, 10)
) %>% 
  dplyr::group_by(grp) %>% 
  dplyr::summarize(data = list(values)) %>% 
  gt() %>% 
  gt_plt_bar_stack(data, palette = c("black", "orange"),
                   labels = c("Grp 1", "Grp 2")) %>% 
  gtsave("test.png")

Created on 2021-10-15 by the reprex package (v2.0.1)

rizqrama commented 2 years ago

For your issues, here's a quick reprex:

library(gtExtras)
library(gt)

packageVersion("gtExtras")
#> [1] '0.2.19'

generate_df(
  n = 2, n_grps = 10, mean = rep(400000, 10)
) %>% 
  dplyr::group_by(grp) %>% 
  dplyr::summarize(data = list(values)) %>% 
  gt() %>% 
  gt_plt_bar_stack(data, palette = c("black", "orange"),
                   labels = c("Grp 1", "Grp 2")) %>% 
  gtsave("test.png")

Created on 2021-10-15 by the reprex package (v2.0.1)

Thank you very much for your explanation!