Some tables, like correlation matrices, can be standardized and automated. Others are uniquely tied to a specific purpose, and no function could anticipate their structure. The apa7 package has a few functions to automate what can be automated and a styling function to make the final styling of a complex table a little easier.
By default, the output of apa_cor
flextable, with significant correlations bolded.
trees |>
apa_cor(star_significant = T)
apa_cor
Variable |
M |
SD |
1 |
2 |
3 |
|||
---|---|---|---|---|---|---|---|---|
1. Girth |
13.25 |
3.14 |
— |
|||||
2. Height |
76.00 |
6.37 |
.52 |
** |
— |
|||
3. Volume |
30.17 |
16.44 |
.97 |
*** |
.60 |
*** |
— |
|
** p < .01. *** p < .001 |
Significant correlations can be starred instead.
trees |>
apa_cor(star_significant = TRUE)
Variable |
M |
SD |
1 |
2 |
3 |
|||
---|---|---|---|---|---|---|---|---|
1. Girth |
13.25 |
3.14 |
— |
|||||
2. Height |
76.00 |
6.37 |
.52 |
** |
— |
|||
3. Volume |
30.17 |
16.44 |
.97 |
*** |
.60 |
*** |
— |
|
** p < .01. *** p < .001 |
If you want to use another table package, you can output a tibble instead and then process the table however you want.
kable
via apa_cor
Variable | M | SD | 1 | 2 | 3 |
---|---|---|---|---|---|
1. Girth | 13.25 | 3.14 | — | ||
2. Height | 76.00 | 6.37 | .52 | — | |
3. Volume | 30.17 | 16.44 | .97 | .60 | — |
Styling options
Your styling options include:
-
note
A custom markdown formatted note. Overrides automatic note about bolded correlations being significant. Default:NULL
-
p_value
Significance value. Default:.05
-
digits
Number of digits for rounding. Default:2
-
bold_significant
Whether to bold significant correlations. Default:TRUE
-
star_significant
Whether to star significant correlations. Default:FALSE
-
significance_note
Whether note about bolding correlations appears. Overridden by a custom note. Also, the automatic note will not appear if no correlations are significant. Default:TRUE
-
output
Options areflextable
,gt
ortibble
. Default:flextable
-
family
Font family (typeface). Default:Times New Roman
-
font_size
Font size (in points). Default:12
-
text_color
Text color. Default:black
-
border_color
Table border color. Default:black
-
border_width
Table border width. Default:0.5
-
line_spacing
Text line spacing. Default:2
For example,
# List of descriptive functions
my_summaries <- list(
n = function(x) sum(!is.na(x)),
M = mean,
SD = sd,
Skewness = psych::skew,
Kurtosis = psych::kurtosi)
trees |>
apa_cor(p_value = .001,
digits = 2,
star_significant = TRUE,
significance_note = TRUE,
summary_functions = my_summaries,
font_family = "Arial",
font_size = 10,
text_color = "darkred",
border_color = "royalblue4",
border_width = 1,
line_spacing = 2)
Variable |
n |
M |
SD |
Skewness |
Kurtosis |
1 |
2 |
3 |
|||
---|---|---|---|---|---|---|---|---|---|---|---|
1. Girth |
31 |
13.25 |
3.14 |
0.50 |
−0.71 |
— |
|||||
2. Height |
31 |
76.00 |
6.37 |
−0.36 |
−0.72 |
.52 |
— |
||||
3. Volume |
31 |
30.17 |
16.44 |
1.01 |
0.25 |
.97 |
* |
.60 |
* |
— |
|
* p < .001 |
To remove summary statistics entirely, set summary_functions
to NA
or NULL
.
x <- apa_cor(trees,
summary_functions = NA,
bold_significant = FALSE)
Customization
The styling options for apa_cor
are minimal, but flexable allows for all kinds of customization. Just place whatever code from flextable or gt you want after apa_cor
. For example, gt tables usually span 100% of the width in a space. Here I set the width to 50% and align the table to the left of the page. I place a footnote in a specific cell in the table body.
apa_cor(trees) |>
flextable::footnote(
i = 2,
j = "Variable",
value = flextable::as_paragraph("This is a footnote."),
part = "body"
) |>
flextable::set_table_properties(
width = .5,
align = "left")
Variable |
M |
SD |
1 |
2 |
3 |
---|---|---|---|---|---|
1. Girth |
13.25 |
3.14 |
— |
||
2. Height1 |
76.00 |
6.37 |
.52 |
— |
|
3. Volume |
30.17 |
16.44 |
.97 |
.60 |
— |
Note. Correlations significant at p < .05 are bolded. | |||||
1This is a footnote. |