jthomasmock / gtExtras

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

Disproportional bar length in `gt_plt_bullet()` #87

Closed marianschmidt closed 1 year ago

marianschmidt commented 1 year ago

In the latest dev version, I still found an issue where gt_plt_bullet() does not show the correct bar length or target value. I adapted the example shown in #79.

library(tibble)
library(gt)
library(gtExtras)

df <- tibble(A = c(.71, .78), B = c(1.01, 1.01)) %>%
  mutate(value = A, target = B, .before = A)

#resulting plot does not show bars proportionally 
df %>% 
  gt() %>% 
  gt_plt_bullet(column = A, target = B)

image

Created on 2023-04-06 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.3 (2023-03-15 ucrt) #> os Windows 10 x64 (build 19045) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate German_Germany.utf8 #> ctype German_Germany.utf8 #> tz Europe/Berlin #> date 2023-04-06 #> pandoc 2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.2.3) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.2.2) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.2) #> dplyr 1.1.1 2023-03-22 [1] CRAN (R 4.2.3) #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.2) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.2) #> farver 2.1.1 2022-07-06 [1] CRAN (R 4.2.1) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.2) #> fontawesome 0.5.0 2023-01-25 [1] CRAN (R 4.2.2) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.2) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.1) #> ggplot2 3.4.2 2023-04-03 [1] CRAN (R 4.2.2) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.1) #> gt * 0.9.0 2023-03-31 [1] CRAN (R 4.2.2) #> gtable 0.3.3 2023-03-21 [1] CRAN (R 4.2.2) #> gtExtras * 0.4.6.9001 2023-04-05 [1] Github (jthomasmock/gtExtras@95f7bf7) #> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.2.3) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.2) #> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.1) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.1) #> paletteer 1.5.0 2022-10-19 [1] CRAN (R 4.2.1) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.2.2) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.1) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.2) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.2) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.1) #> ragg 1.2.5 2023-01-12 [1] CRAN (R 4.2.2) #> rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.2.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1) #> rlang 1.1.0 2023-03-14 [1] CRAN (R 4.2.2) #> rmarkdown 2.21 2023-03-26 [1] CRAN (R 4.2.2) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1) #> sass 0.4.5 2023-01-24 [1] CRAN (R 4.2.2) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.1) #> styler 1.9.1 2023-03-04 [1] CRAN (R 4.2.2) #> svglite 2.1.1 2023-01-10 [1] CRAN (R 4.2.2) #> systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.2.1) #> textshaping 0.3.6 2021-10-13 [1] CRAN (R 4.2.1) #> tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.2.2) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.1) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.2) #> vctrs 0.6.1 2023-03-22 [1] CRAN (R 4.2.3) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.1) #> xfun 0.38 2023-03-24 [1] CRAN (R 4.2.3) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.1) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.2) #> #> [1] C:/Users/ga27jar/AppData/Local/R/win-library/4.2 #> [2] C:/Program Files/R/R-4.2.3/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
marianschmidt commented 1 year ago

I think, a potential fix either needs a switch in

https://github.com/jthomasmock/gtExtras/blob/95f7bf7a227f36528abcd3b30d309e119ef5e4a5/R/gt_plt_bullet.R#L45

for the minimum to be either 0 or the minimum value (if negative) of the range.

Alternatively, users could also specify a custom range in the function that is then applied to line

https://github.com/jthomasmock/gtExtras/blob/95f7bf7a227f36528abcd3b30d309e119ef5e4a5/R/gt_plt_bullet.R#L89

and could then take the default as suggested above.

marianschmidt commented 1 year ago

so a new function parameter could be xlim = c(min(c(0,all_vals, target_vals), na.rm = TRUE) , max(c(all_vals, target_vals), na.rm = TRUE) and line 89 could change to coord_cartesian(xlim = xlim) +. That's the hot fix I have applied now.

jthomasmock commented 1 year ago

Made an accommodation to the range to force a zero range if not negative. Will push latest dev version shortly.

library(tibble)
library(gt)
library(gtExtras)

df <- tibble(A = c(.71, .78), B = c(1.01, 1.01)) %>%
  mutate(value = A, target = B, .before = A)

#resulting plot does not show bars proportionally 
df %>% 
  gt() %>% 
  gt_plt_bullet(column = A, target = B) |> 
  gtExtras::gt_reprex_image()

Created on 2023-04-11 by the reprex package (v2.0.1)

library(tibble)
library(gt)
library(gtExtras)

df <- tibble(A = c(1:2, -3, 4:5), B = c(1:2, -3, 4:5), C = c(3,4,5,6,7))

df %>% 
  gt() %>% 
  gt_plt_bullet(column = B, target = C) %>%
  gtExtras::gt_reprex_image()

Created on 2023-04-11 by the reprex package (v2.0.1)