jthomasmock / gtExtras

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

`gt_plot_bar_pct()` plots `NA` values #86

Closed jmbarbone closed 1 year ago

jmbarbone commented 1 year ago

Behaviors of gt_plt_bar() and gt_plot_bar_pct() differ when NA values are present. The former skips over these (correctly, it seems) but the latter does not skip these and rather scales to the highest value.

library(gt)
library(gtExtras)

data <- data.frame(
  id = 1:3,
  a = 1:3,
  b = c(1, NA, 3), 
  c = rep(NA, 3)
)

data |> 
  gt() |> 
  gt_plt_bar(a) |> 
  gt_plt_bar(b) |> 
  gt_plt_bar(c)
#> Warning in min(all_vals, na.rm = TRUE): no non-missing arguments to min;
#> returning Inf
#> Warning in max(all_vals, na.rm = TRUE): no non-missing arguments to max;
#> returning -Inf
#> Warning: Removed 1 rows containing missing values (`position_stack()`).
#> Warning: Removed 1 rows containing missing values (`geom_vline()`).
#> Warning: Removed 1 rows containing missing values (`position_stack()`).
#> Warning: Removed 1 rows containing missing values (`geom_vline()`).
#> Warning: Removed 1 rows containing missing values (`position_stack()`).
#> Warning: Removed 1 rows containing missing values (`geom_vline()`).

image

data |>
  gt() |>
  gt_plt_bar_pct(a) |>
  gt_plt_bar_pct(b) |>
  gt_plt_bar_pct(c) |>
  # need some space for the columns to show
  cols_label(a = "aaaaa", b = "bbbbb", c = "ccccc")
#> Warning in fn(body[[col]][stub_df$rownum_i %in% loc$rows]): NAs introduced by
#> coercion
#> Warning in max(as.double(x), na.rm = TRUE): no non-missing arguments to max;
#> returning -Inf

image

Created on 2023-03-22 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 19044) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz America/New_York #> date 2023-03-22 #> 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.0 2023-01-09 [1] CRAN (R 4.2.2) #> 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.0 2023-01-29 [1] CRAN (R 4.2.2) #> 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.1 2023-02-10 [1] CRAN (R 4.2.2) #> D glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.1) #> gt * 0.8.0 2022-11-16 [1] CRAN (R 4.2.2) #> gtable 0.3.3 2023-03-21 [1] CRAN (R 4.2.3) #> gtExtras * 0.4.6.9001 2023-03-22 [1] Github (jthomasmock/gtExtras@95f7bf7) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.2) #> 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.9000 2022-10-19 [1] Github (r-lib/lifecycle@80a1e52) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0) #> paletteer 1.5.0 2022-10-19 [1] any (@1.5.0) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.2) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.2) #> R.cache 0.16.0 2022-07-21 [1] any (@0.16.0) #> 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.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> 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.0) #> reprex 2.0.2 2022-08-17 [1] any (@2.0.2) #> rlang 1.1.0 2023-03-14 [1] CRAN (R 4.2.3) #> rmarkdown 2.20 2023-01-19 [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.0) #> 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.0) #> textshaping 0.3.6 2021-10-13 [1] CRAN (R 4.2.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.2.3) #> 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.0 2023-03-16 [1] CRAN (R 4.2.3) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.37 2023-01-31 [1] CRAN (R 4.2.2) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.2) #> #> [1] C:/Users/jbarbone/OneDrive - Cogstate/Documents/R/win-library/4.2 #> [2] C:/Program Files/R/R-4.2.3/library #> #> D ── DLL MD5 mismatch, broken installation. #> #> ────────────────────────────────────────────────────────────────────────────── ```
jthomasmock commented 1 year ago

Thanks! Latest version will fix this, will push shortly.

library(gt)
library(gtExtras)

data <- data.frame(
  id = 1:3,
  a = 1:3,
  b = c(1, NA, 3), 
  c = rep(NA, 3)
)

data |> 
  gt() |> 
  gt_plt_bar(a) |> 
  gt_plt_bar(b) |> 
  gt_plt_bar(c) |> 
  gt_reprex_image()

data |> 
  gt() |> 
  cols_label(a = "aaaaa", b = "bbbbb", c = "ccccc") |> 
  gt_plt_bar_pct(a) |> 
  gt_plt_bar_pct(b) |> 
  gt_plt_bar_pct(c) |> 
  gt_reprex_image()

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

melindahiggins2000 commented 11 months ago

I am having an issue with this example. I actually get the full bar for NAs using gt_plt_bar_pct for the NA in the "b" column. Here is my code as listed above and the plot I am getting.

I have the current version of gt and gtExtras.

Is there another dependency I am missing?

library(gt)
library(gtExtras)

data <- data.frame(
  id = 1:3,
  a = 1:3,
  b = c(1, NA, 3), 
  c = rep(NA, 3)
)

data |> 
  gt() |> 
  cols_label(a = "aaaaa", b = "bbbbb", c = "ccccc") |> 
  gt_plt_bar_pct(a) |> 
  gt_plt_bar_pct(b) |> 
  gt_plt_bar_pct(c)

image

My sessionInfo() is as follows:

> sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] gtExtras_0.5.0.9002 gt_0.10.0          

loaded via a namespace (and not attached):
 [1] sass_0.4.6         utf8_1.2.3         generics_0.1.3    
 [4] xml2_1.3.3         stringi_1.7.12     extrafontdb_1.0   
 [7] digest_0.6.29      magrittr_2.0.3     grid_4.3.1        
[10] RColorBrewer_1.1-3 fastmap_1.1.1      plyr_1.8.8        
[13] rematch2_2.1.2     ggtext_0.1.2       gridExtra_2.3     
[16] fansi_1.0.4        waffle_1.0.2       scales_1.2.1      
[19] textshaping_0.3.6  cli_3.6.1          rlang_1.1.1       
[22] munsell_0.5.0      withr_2.5.0        tools_4.3.1       
[25] dplyr_1.1.3        colorspace_2.0-3   ggplot2_3.4.3     
[28] DT_0.29            curl_5.0.2         paletteer_1.4.1   
[31] vctrs_0.6.3        R6_2.5.1           lifecycle_1.0.3   
[34] stringr_1.5.0      htmlwidgets_1.6.2  ragg_1.2.5        
[37] fontawesome_0.5.2  pkgconfig_2.0.3    pillar_1.9.0      
[40] gtable_0.3.4       glue_1.6.2         Rcpp_1.0.11       
[43] systemfonts_1.0.4  xfun_0.40          tibble_3.2.1      
[46] tidyselect_1.2.0   rstudioapi_0.15.0  knitr_1.44        
[49] farver_2.1.1       extrafont_0.19     htmltools_0.5.6   
[52] labeling_0.4.3     svglite_2.1.1      Rttf2pt1_1.3.12   
[55] compiler_4.3.1     gridtext_0.1.5 

Thank you for your time and help.