Correlation matrices look simple enough…until you try to make one by hand in MS Word. The apa_cor function can remove most of the tedium by formatting most of what can automated, and any finishing touches can be added with a styling function.
The examples use the epi.bfi data set from the psychTools package (Revelle, 2019), which has personality, depression, and anxiety scores for 231 participants in studies at Northwestern University’s Personality, Motivation, and Cognition Laboratory. Openness, conscientiousness, extraversion, agreeableness, and neuroticism are Big Five scale scores, and depression is the Beck Depression Inventory score.
apa_cor returns a table that is displayed in APA style in documents, with significant correlations starred. In the console, it is an ordinary tibble.
tbl_cor <- apa_cor(d_personality)
tbl_corTable 1
Correlations Among Personality and Depression
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
123.43 |
20.51 |
— |
|||||||||||
2. |
Conscientiousness |
113.25 |
21.88 |
.31 |
*** |
— |
|||||||||
3. |
Extraversion |
102.18 |
26.45 |
.46 |
*** |
.27 |
*** |
— |
|||||||
4. |
Agreeableness |
125.00 |
18.14 |
.39 |
*** |
.45 |
*** |
.48 |
*** |
— |
|||||
5. |
Neuroticism |
87.97 |
23.34 |
.29 |
*** |
.04 |
.04 |
−.04 |
— |
||||||
6. |
Depression |
6.78 |
5.78 |
−.08 |
−.18 |
** |
−.14 |
* |
−.14 |
* |
.47 |
*** |
— |
||
apa_note writes the table’s note. The text is in markdown, and it does not start with “Note.” because apaquarto adds that:
apa_note(tbl_cor)[1] "^\\* ^*p* ≤ .05. ^\\*\\* ^*p* ≤ .01. ^\\*\\*\\* ^*p* ≤ .001."
With apaquarto, use apa_note in the apa-note chunk option (e.g., #| apa-note: !expr apa7::apa_note(tbl_cor)), with the table made in an earlier chunk. Without apaquarto, apa_flextable(note = TRUE) puts the note below the table, as in the rest of this article:
apa_flextable(tbl_cor, note = TRUE)Table 2
Correlations Among Personality and Depression With a Note
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
123.43 |
20.51 |
— |
|||||||||||
2. |
Conscientiousness |
113.25 |
21.88 |
.31 |
*** |
— |
|||||||||
3. |
Extraversion |
102.18 |
26.45 |
.46 |
*** |
.27 |
*** |
— |
|||||||
4. |
Agreeableness |
125.00 |
18.14 |
.39 |
*** |
.45 |
*** |
.48 |
*** |
— |
|||||
5. |
Neuroticism |
87.97 |
23.34 |
.29 |
*** |
.04 |
.04 |
−.04 |
— |
||||||
6. |
Depression |
6.78 |
5.78 |
−.08 |
−.18 |
** |
−.14 |
* |
−.14 |
* |
.47 |
*** |
— |
||
Note. * p ≤ .05. ** p ≤ .01. *** p ≤ .001. | |||||||||||||||
Significant correlations can be bolded instead.
d_personality |>
apa_cor(bold_significant = TRUE) |>
apa_flextable(note = TRUE)Table 3
Correlations With Significant Correlations in Bold
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
|
|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
123.43 |
20.51 |
— |
|||||
2. |
Conscientiousness |
113.25 |
21.88 |
.31 |
— |
||||
3. |
Extraversion |
102.18 |
26.45 |
.46 |
.27 |
— |
|||
4. |
Agreeableness |
125.00 |
18.14 |
.39 |
.45 |
.48 |
— |
||
5. |
Neuroticism |
87.97 |
23.34 |
.29 |
.04 |
.04 |
−.04 |
— |
|
6. |
Depression |
6.78 |
5.78 |
−.08 |
−.18 |
−.14 |
−.14 |
.47 |
— |
Note. Correlations significant at p ≤ .05 are bolded. | |||||||||
Summary functions
You can include any named list of summary functions:
# Named list of descriptive functions
my_summaries <- list(Mdn = median, "*IQR*" = IQR)
apa_cor(d_personality,
summary_functions = my_summaries,
star_significant = FALSE) |>
apa_flextable(note = TRUE)Table 4
Correlations Among Personality and Depression With Medians and Interquartile Ranges
Variable |
Mdn |
IQR |
1 |
2 |
3 |
4 |
5 |
6 |
|
|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
125 |
26.50 |
— |
|||||
2. |
Conscientiousness |
114 |
29.50 |
.31 |
— |
||||
3. |
Extraversion |
104 |
30.50 |
.46 |
.27 |
— |
|||
4. |
Agreeableness |
126 |
24.50 |
.39 |
.45 |
.48 |
— |
||
5. |
Neuroticism |
90 |
34.00 |
.29 |
.04 |
.04 |
−.04 |
— |
|
6. |
Depression |
6 |
6.00 |
−.08 |
−.18 |
−.14 |
−.14 |
.47 |
— |
You can also use a plain vector of descriptive functions from these options: "IQR", "Interquartile Range", "Kurtosis", "MAD", "Median Absolute Deviation", "M", "Mean", "Med", "Median", "n", "N", "Quantile", "Range", "SD", "Skewness", "Var", "Variance"
my_summaries <- c("n", "M", "SD", "Skewness", "Kurtosis")
apa_cor(d_personality, summary_functions = my_summaries) |>
apa_flextable(note = TRUE)Table 5
Correlations Among Personality and Depression With Additional Summary Statistics
Variable |
n |
M |
SD |
Skewness |
Kurtosis |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
231 |
123.43 |
20.51 |
−0.16 |
−0.16 |
— |
|||||||||||
2. |
Conscientiousness |
231 |
113.25 |
21.88 |
−0.02 |
0.23 |
.31 |
*** |
— |
|||||||||
3. |
Extraversion |
231 |
102.18 |
26.45 |
−0.41 |
0.51 |
.46 |
*** |
.27 |
*** |
— |
|||||||
4. |
Agreeableness |
231 |
125.00 |
18.14 |
−0.21 |
−0.27 |
.39 |
*** |
.45 |
*** |
.48 |
*** |
— |
|||||
5. |
Neuroticism |
231 |
87.97 |
23.34 |
0.07 |
−0.55 |
.29 |
*** |
.04 |
.04 |
−.04 |
— |
||||||
6. |
Depression |
231 |
6.78 |
5.78 |
1.29 |
1.50 |
−.08 |
−.18 |
** |
−.14 |
* |
−.14 |
* |
.47 |
*** |
— |
||
Note. * p ≤ .05. ** p ≤ .01. *** p ≤ .001. | ||||||||||||||||||
Options
apa_cor decides what the table shows:
-
alphap-value thresholds for one, two, and three stars (withbold_significant = TRUE, only the first is used). Default:c(.05, .01, .001) -
digitsNumber of decimal places. Default:2 -
bold_significantWhether to bold significant correlations instead of starring them. Default:FALSE -
star_significantWhether to star significant correlations. Default:TRUE -
significance_noteWhether the note explains the stars (or bolding). The note does not explain them if no correlations are significant. Default:TRUE -
keep_empty_star_columnsWhether correlation columns without significant correlations keep their star columns, so that all correlation columns have the same width. Default:TRUE
apa_flextable decides how it looks:
-
noteTRUEfor the note fromapa_note, or a markdown-formatted note of your own. Default:NULL(no note) -
font_familyFont family (typeface). Default:Times New Roman -
font_sizeFont size (in points). Default:12 -
text_colorText color. Default:black -
border_colorTable border color. Default:black -
border_widthTable border width (in points). Default:0.5 -
line_spacingText line spacing. Default:2 -
table_widthWidth of the table (e.g.,.5for half the page width). Default: the full width of the page
The defaults of the styling options can be set for all tables with apa7_defaults. For example,
d_personality |>
apa_cor(
alpha = 0.001,
digits = 2,
bold_significant = TRUE,
significance_note = TRUE,
summary_functions = my_summaries
) |>
apa_flextable(
note = TRUE,
font_family = "Arial",
font_size = 10,
text_color = "darkred",
border_color = "royalblue4",
border_width = 1,
line_spacing = 2
)Table 6
Correlations With Alternative Styling
Variable |
n |
M |
SD |
Skewness |
Kurtosis |
1 |
2 |
3 |
4 |
5 |
6 |
|
|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
231 |
123.43 |
20.51 |
−0.16 |
−0.16 |
— |
|||||
2. |
Conscientiousness |
231 |
113.25 |
21.88 |
−0.02 |
0.23 |
.31 |
— |
||||
3. |
Extraversion |
231 |
102.18 |
26.45 |
−0.41 |
0.51 |
.46 |
.27 |
— |
|||
4. |
Agreeableness |
231 |
125.00 |
18.14 |
−0.21 |
−0.27 |
.39 |
.45 |
.48 |
— |
||
5. |
Neuroticism |
231 |
87.97 |
23.34 |
0.07 |
−0.55 |
.29 |
.04 |
.04 |
−.04 |
— |
|
6. |
Depression |
231 |
6.78 |
5.78 |
1.29 |
1.50 |
−.08 |
−.18 |
−.14 |
−.14 |
.47 |
— |
Note. Correlations significant at p ≤ .001 are bolded. | ||||||||||||
To remove summary statistics entirely, set summary_functions to NA or NULL.
apa_cor(d_personality, summary_functions = NA) |>
apa_flextable(note = TRUE)Table 7
Correlations Without Summary Statistics
Variable |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
— |
|||||||||||
2. |
Conscientiousness |
.31 |
*** |
— |
|||||||||
3. |
Extraversion |
.46 |
*** |
.27 |
*** |
— |
|||||||
4. |
Agreeableness |
.39 |
*** |
.45 |
*** |
.48 |
*** |
— |
|||||
5. |
Neuroticism |
.29 |
*** |
.04 |
.04 |
−.04 |
— |
||||||
6. |
Depression |
−.08 |
−.18 |
** |
−.14 |
* |
−.14 |
* |
.47 |
*** |
— |
||
Note. * p ≤ .05. ** p ≤ .01. *** p ≤ .001. | |||||||||||||
Customization
The styling options of apa_flextable are minimal, but flextable allows for all kinds of customization. Just place whatever code from flextable you want after apa_flextable. Here I set the width to 50%, and I place a footnote in a specific cell in the table body.
apa_cor(d_personality) |>
apa_flextable(table_width = .5) |>
flextable::footnote(
i = 2,
j = "Variable",
value = flextable::as_paragraph("This is a footnote."),
part = "body"
)Table 8
Correlations Styled With flextable Functions
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
123.43 |
20.51 |
— |
|||||||||||
2. |
Conscientiousness1 |
113.25 |
21.88 |
.31 |
*** |
— |
|||||||||
3. |
Extraversion |
102.18 |
26.45 |
.46 |
*** |
.27 |
*** |
— |
|||||||
4. |
Agreeableness |
125.00 |
18.14 |
.39 |
*** |
.45 |
*** |
.48 |
*** |
— |
|||||
5. |
Neuroticism |
87.97 |
23.34 |
.29 |
*** |
.04 |
.04 |
−.04 |
— |
||||||
6. |
Depression |
6.78 |
5.78 |
−.08 |
−.18 |
** |
−.14 |
* |
−.14 |
* |
.47 |
*** |
— |
||
1This is a footnote. | |||||||||||||||
Changing the tibble
Because apa_cor returns a tibble, you can change it with dplyr and tidyr functions before passing it to apa_flextable (or to a table function from another package). Here we add a row title.
apa_cor(d_personality) |>
mutate(apa7listcolumn = str_trim(apa7listcolumn)) |>
unite(col = Variable,
apa7listcolumn,
Variable,
sep = " ") |>
add_row(Variable = "Row title", .before = 1) |>
apa_flextable(no_format_columns = "Variable") |>
padding(i = 2:7,
j = 1,
padding.left = 15)Table 9
Correlations With a Row Title
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Row title |
||||||||||||||
1. Openness |
123.43 |
20.51 |
— |
|||||||||||
2. Conscientiousness |
113.25 |
21.88 |
.31 |
*** |
— |
|||||||||
3. Extraversion |
102.18 |
26.45 |
.46 |
*** |
.27 |
*** |
— |
|||||||
4. Agreeableness |
125.00 |
18.14 |
.39 |
*** |
.45 |
*** |
.48 |
*** |
— |
|||||
5. Neuroticism |
87.97 |
23.34 |
.29 |
*** |
.04 |
.04 |
−.04 |
— |
||||||
6. Depression |
6.78 |
5.78 |
−.08 |
−.18 |
** |
−.14 |
* |
−.14 |
* |
.47 |
*** |
— |
Other Kinds of Correlations
apa_cor computes the correlations with correlation::correlation from the correlation package: by default, Pearson correlations with unadjusted p-values. Depression scores are skewed, as the skewness in the summary statistics above shows, so rank correlations are a reasonable alternative. The method argument chooses the kind of correlation. For example, Spearman rank correlations:
apa_cor(d_personality, method = "spearman") |>
apa_flextable(note = TRUE)Table 10
Spearman Rank Correlations Among Personality and Depression
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
123.43 |
20.51 |
— |
|||||||||||
2. |
Conscientiousness |
113.25 |
21.88 |
.32 |
*** |
— |
|||||||||
3. |
Extraversion |
102.18 |
26.45 |
.43 |
*** |
.25 |
*** |
— |
|||||||
4. |
Agreeableness |
125.00 |
18.14 |
.39 |
*** |
.43 |
*** |
.50 |
*** |
— |
|||||
5. |
Neuroticism |
87.97 |
23.34 |
.28 |
*** |
.04 |
.05 |
−.04 |
— |
||||||
6. |
Depression |
6.78 |
5.78 |
−.10 |
−.16 |
* |
−.13 |
* |
−.11 |
.47 |
*** |
— |
|||
Note. Values are Spearman correlations. * p ≤ .05. *** p ≤ .001. | |||||||||||||||
The note says what kind of correlations are shown. Other options for method include:
| Option | Correlation |
|---|---|
pearson |
Pearson’s Correlation (default) |
spearman |
Spearman’s Rank Correlation |
kendall |
Kendall’s Rank Correlation |
biserial |
Biserial Correlation |
polychoric |
Polychoric Correlation |
tetrachoric |
Tetrachoric Correlation |
biweight |
Biweight midcorrelation |
distance |
Distance Correlation |
percentage |
Percentage Bend Correlation |
blomqvist |
Blomqvist’s coefficient |
hoeffding |
Hoeffding’s D |
gamma |
Gamma Correlation |
gaussian |
for Gaussian Rank correlation |
shepherd |
for Shepherd’s Pi correlation |
To adjust the p-values (and so the stars) for multiple comparisons, use p_adjust (e.g., p_adjust = "holm"), and the note says how they were adjusted. Partial correlations, each controlling for the other variables in the table, need only partial = TRUE:
apa_cor(d_personality, partial = TRUE) |>
apa_flextable(note = TRUE)Table 11
Partial Correlations Among Personality and Depression
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
123.43 |
20.51 |
— |
|||||||||||
2. |
Conscientiousness |
113.25 |
21.88 |
.10 |
— |
||||||||||
3. |
Extraversion |
102.18 |
26.45 |
.31 |
*** |
.01 |
— |
||||||||
4. |
Agreeableness |
125.00 |
18.14 |
.20 |
** |
.35 |
*** |
.33 |
*** |
— |
|||||
5. |
Neuroticism |
87.97 |
23.34 |
.37 |
*** |
.10 |
−.02 |
−.15 |
* |
— |
|||||
6. |
Depression |
6.78 |
5.78 |
−.17 |
* |
−.16 |
* |
−.06 |
.05 |
.51 |
*** |
— |
|||
Note. Values are partial Pearson correlations, each controlling for all other variables in the table. * p ≤ .05. ** p ≤ .01. *** p ≤ .001. | |||||||||||||||
Partial correlations can differ considerably from ordinary correlations. With zero-order correlations, openness and extraversion correlate at .27, but once other personality variables are held constant, the partial correlation between them is close to zero.
Other arguments of correlation::correlation (e.g., robust = TRUE, winsorize = .2, or multilevel = TRUE) can be given to apa_cor as well. Variable names with spaces work for every kind of correlation, because apa_cor gives the variables internal names while it computes the correlations. (Called directly, correlation::correlation does not accept such names for partial or multilevel correlations.)
The output of correlation::correlation can also be given to apa_cor:
correlation(d_personality, method = "spearman") |>
apa_cor() |>
apa_flextable(note = TRUE)Table 12
Spearman Rank Correlations Computed by the correlation Package
Variable |
M |
SD |
1 |
2 |
3 |
4 |
5 |
6 |
|||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. |
Openness |
123.43 |
20.51 |
— |
|||||||||||
2. |
Conscientiousness |
113.25 |
21.88 |
.32 |
*** |
— |
|||||||||
3. |
Extraversion |
102.18 |
26.45 |
.43 |
*** |
.25 |
*** |
— |
|||||||
4. |
Agreeableness |
125.00 |
18.14 |
.39 |
*** |
.43 |
*** |
.50 |
*** |
— |
|||||
5. |
Neuroticism |
87.97 |
23.34 |
.28 |
*** |
.04 |
.05 |
−.04 |
— |
||||||
6. |
Depression |
6.78 |
5.78 |
−.10 |
−.16 |
−.13 |
−.11 |
.47 |
*** |
— |
|||||
Note. Values are Spearman correlations. p-values are adjusted for multiple comparisons with Holm’s method (Holm, 1979). *** p ≤ .001. | |||||||||||||||
Here, correlation::correlation adjusted the p-values with Holm’s method, its default, so the stars are for the adjusted p-values, and the note says so. The means and standard deviations come from the data that correlation::correlation keeps with its results. Correlations computed by group, between two sets of variables, or with Bayesian methods are not yet supported.