ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.04k stars 114 forks source link

Add `add_stat_label()` to the JAMA theme for `tbl_svysummary()` output #1964

Open jaromilfrossard opened 6 days ago

jaromilfrossard commented 6 days ago

Hello,

Thank you for creating gtsummary!

It seems that the theme for journals does not apply correctly to the tbl_svysummary() object as it stays the default.

However, it seems that the "compact" theme worked as I expected.

I am using the last CRAN version of gtsummary (2.0.2), with R 4.4.0, on windows.

Thanks you again!

PROBLEM: default theme

# insert reprex here
library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.4.1

theme_gtsummary_journal("jama")
#> Setting theme "JAMA"
survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) |>
  tbl_svysummary(by = Survived, percent = "row", include = c(Class, Age))

OK : JAMA theme

library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.4.1

theme_gtsummary_journal("jama")

#> Setting theme "JAMA"
as.data.frame(Titanic)|>
  tbl_summary(by = Survived, percent = "row", include = c(Class, Age))

PROBLEM: default + compact theme

library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.4.1
theme_gtsummary_compact()
#> Setting theme "Compact"
theme_gtsummary_journal("jama")
#> Setting theme "JAMA"
survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) |>
  tbl_svysummary(by = Survived, percent = "row", include = c(Class, Age))

OK: JAMA + compact theme

  library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.4.1
  theme_gtsummary_compact()
#> Setting theme "Compact"
  theme_gtsummary_journal("jama")
#> Setting theme "JAMA"
as.data.frame(Titanic)|>
  tbl_summary(by = Survived, percent = "row", include = c(Class, Age))
ddsjoberg commented 6 days ago

Dear @jaromilfrossard , Thanks for the post.

Can you make a couple of updates/add more information?

jaromilfrossard commented 5 days ago

Hello,

The difference is in the display of the statistics. From my understanding the JAMA theme displays the statistics with the variable name (eg: "Class, n (%)") not as reference in the footnote.

However, this feature does not seem to apply with tbl_svysummary(). It is visible in the last kable, the "quoted" line is "|Class |NA |NA |", but I would expect "|Class, n (%) |NA |NA |".

I hope it is more clear now.

  library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.4.1

  reset_gtsummary_theme()
# default theme
survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) |>
  tbl_svysummary(by = Survived, percent = "row", include = c(Class, Age)) |> 
  knitr::kable()

|Characteristic |No
N = 1,490 |Yes
N = 711 | |:——————|:—————–|:—————-| |Class |NA |NA | |1st |122 (38%) |203 (62%) | |2nd |167 (59%) |118 (41%) | |3rd |528 (75%) |178 (25%) | |Crew |673 (76%) |212 (24%) | |Age |NA |NA | |Child |52 (48%) |57 (52%) | |Adult |1,438 (69%) |654 (31%) |


as.data.frame(Titanic)|>
  tbl_summary(by = Survived, percent = "row", include = c(Class, Age)) |> 
  knitr::kable()

|Characteristic |No
N = 16 |Yes
N = 16 | |:——————|:————–|:—————| |Class |NA |NA | |1st |4 (50%) |4 (50%) | |2nd |4 (50%) |4 (50%) | |3rd |4 (50%) |4 (50%) | |Crew |4 (50%) |4 (50%) | |Age |NA |NA | |Child |8 (50%) |8 (50%) | |Adult |8 (50%) |8 (50%) |


theme_gtsummary_journal("jama")
#> Setting theme "JAMA"

# Here it displays the statistics with the variable: "Class, n (%)"
as.data.frame(Titanic)|>
  tbl_summary(by = Survived, percent = "row", include = c(Class, Age)) |> 
  knitr::kable()

|Characteristic |No
N = 16 |Yes
N = 16 | |:——————|:————–|:—————| |Class, n (%) |NA |NA | |1st |4 (50) |4 (50) | |2nd |4 (50) |4 (50) | |3rd |4 (50) |4 (50) | |Crew |4 (50) |4 (50) | |Age, n (%) |NA |NA | |Child |8 (50) |8 (50) | |Adult |8 (50) |8 (50) |


# Here the statistic is not displayed: "Class, n (%)"
survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) |>
  tbl_svysummary(by = Survived, percent = "row", include = c(Class, Age)) |> 
  knitr::kable()

|Characteristic |No
N = 1,490 |Yes
N = 711 | |:——————|:—————–|:—————-|

|Class |NA |NA |

|1st |122 (38) |203 (62) | |2nd |167 (59) |118 (41) | |3rd |528 (75) |178 (25) | |Crew |673 (76) |212 (24) | |Age |NA |NA | |Child |52 (48) |57 (52) | |Adult |1,438 (69) |654 (31) |

Created on 2024-09-13 with reprex v2.1.1

ddsjoberg commented 5 days ago

Ah, yes, I see now. Thank you for the report!