Quarto tables, figures, and stats
Chunks can produce figures and tables
```{r}
#| label: tbl-one
#| tbl-cap: "This is a great table"
knitr::kable(mtcars)
```
Chunks can produce figures or tables
```{r}
#| label: fig-hist
#| fig-cap: "This is a histogram"
hist(rnorm(100))
```

Figure 1: This is a histogram
Cross-referencing
You can then refer to those with @tbl-one and @fig-hist and the Table and Figure ordering will be correct (and linked)
@fig-hist contains a histogram and @tbl-one a table.
gets printed as:
Figure 1 contains a histogram and Table 1 a table.
Inline R
Along with just regular text, you can also run R code within the text:
There were `r 3 + 4` participants
becomes:
There were 7 participants
Inline R
This is helpful for reporting statistics, e.g. the sample size:
There were `r nrow(nlsy)` participants
becomes:
There were 1.2686^{4} participants
Inline stats
You can also create an object in a chunk and then reference it later in the text
```{r}
total_sample <- nrow(nlsy)
```
There were `r total_sample` participants
Inline stats (aside)
I often create a list of stats that I want to report in a manuscript:
stats <- list(n = nrow(data),
mean_age = mean(data$age))
I can then print these numbers in the text with:
There were `r stats$n` participants with a mean age of `r stats$mean_age`.
which turns into:
There were 1123 participants with a mean age of 43.5.
Inline stats from {gtsummary}
We saw briefly yesterday:
library(gtsummary)
income_table <- tbl_uvregression(
nlsy,
y = income,
include = c(
sex_cat, race_eth_cat,
eyesight_cat, income, age_bir
),
method = lm
)
inline_text(income_table, variable = "age_bir")
[1] "595 (95% CI 538, 652; p<0.001)"
We pulled a statistic from our univariate table
If we’re making a table, we probably want to report numbers from it
```{r}
#| label: tbl-descr
#| tbl-cap: "Descriptive statistics"
#| output-location: slide
table1 <- tbl_summary(
nlsy,
by = sex_cat,
include = c(sex_cat, race_eth_cat, region_cat,
eyesight_cat, glasses, age_bir)) |>
add_overall(last = TRUE)
table1
```
If we’re making a table, we probably want to report numbers from it
I want to report some stats!
The help file for inline_text() is helpful and tells us that we can look at table1$table_body to help figure out what data to extract.
How about the median (IQR) age of the male participants at the birth of their first child?
inline_text(table1, variable = "age_bir", column = "stat_1")
Formatting
We can add sample sizes for the overall stats on people who wear glasses using the pattern = argument:
inline_text(table1, variable = "glasses", column = "stat_0",
pattern = "{n}/{N} ({p}%)")
Formatting for regression statistics
Remove some details:
inline_text(income_table, variable = "age_bir")
[1] "595 (95% CI 538, 652; p<0.001)"
inline_text(income_table, variable = "age_bir",
pattern = "{estimate} ({conf.low}, {conf.high})")
Better yet…
We can integrate these into the text of our manuscript:
A greater proportion of female (`r inline_text(table1, variable = "glasses", column = "stat_2")`) than male
(`r inline_text(table1, variable = "glasses", column = "stat_1")`) participants wore glasses.
Which becomes:
A greater proportion of female (2,328 (54%)) than male (1,566 (38%)) participants wore glasses.
Readability
Because this can be hard to read, I’d suggest storing those stats in a chunk before the text:
```{r}
glasses_f <- inline_text(table1, variable = "glasses",
column = "stat_2")
glasses_m <- inline_text(table1, variable = "glasses",
column = "stat_1")
```
A greater proportion of female (`r glasses_f`) than male (`r glasses_m`) participants wore glasses.
Exercises
Return to the quarto document with the tables and complete the exercises on the website.