Papaja
papaja (Preparing APA Journal Articles) is an R package that provides document formats to produce complete APA manuscripts from RMarkdown-files (PDF and Word documents) and helper functions that facilitate reporting statistics, tables, and plots.
Install / Use
/learn @crsh/PapajaREADME
<img src='tools/images/papaja_hex.png' align='right' height='150' />papaja: Prepare APA Journal Articles<br />with R Markdown
<!-- README.md is generated from README.Rmd. Please edit that file -->Sections Example | Installation | Usage | Getting help | Citation | papaja in the wild | Computational reproducibility | Contribute | Related R packages | Package dependencies
papaja is an award-winning R package that facilitates creating computationally reproducible, submission-ready manuscripts which conform to the American Psychological Association (APA) manuscript guidelines (6th Edition). papaja provides
- an R Markdown template that can be used with (or without) RStudio to create PDF documents (using the apa6 LaTeX class) or Word documents (using a .docx-reference file).
- Functions to typeset the results from statistical analyses,
- functions to create tables, and
- functions to create figures in accordance with APA guidelines.
For a comprehensive introduction to papaja, see the current draft of the manual. If you have a specific question that is not answered in the manual, feel free to ask a question on Stack Overflow using the papaja tag. If you believe you have found a bug or would like to request a new feature, open an issue on Github and provide a minimal complete verifiable example.
Example
Take a look at the source file of the package vignette and the resulting PDF. The vignette also contains some basic instructions.
Installation
To use papaja you need either a recent version of RStudio or pandoc. If you want to create PDF- in addition to DOCX-documents you additionally need a TeX distribution. We recommend you use TinyTex, which can be installed from within R:
if(!requireNamespace("tinytex", quietly = TRUE)) install.packages("tinytex")
tinytex::install_tinytex()
You may also consider MikTeX for Windows, MacTeX for Mac, or TeX Live for Linux. Please refer to the papaja manual for detailed installation instructions.
papaja is available on CRAN but you can also install it from the GitHub repository:
# Install latest CRAN release
install.packages("papaja")
# Install remotes package if necessary
if(!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")
# Install the stable development version from GitHub
remotes::install_github("crsh/papaja")
Usage
Once papaja is installed, you can select the APA template when creating a new R Markdown file through the RStudio menus.
<figure> <img src="inst/images/template_selection.png" alt="APA template selection dialog" /> <figcaption aria-hidden="true">APA template selection dialog</figcaption> </figure>To add citations, specify your bibliography-file in the YAML front
matter of the document (bibliography: my.bib) and start citing (for
details, see pandoc manual on the citeproc
extension. You may
also be interested in citr, an R
Studio addin to swiftly insert Markdown citations and R Studio’s visual
editor, which also
enables swiftly inserting
citations.
Typeset analysis results
The functions apa_print() and apa_table() facilitate reporting
results of your analyses. When you pass the an output object of a
supported class, such as an htest- or lm-object, to apa_print(),
it will return a list of character strings that you can use to report
the results of your analysis.
my_lm <- lm(
Sepal.Width ~ Sepal.Length + Petal.Width + Petal.Length
, data = iris
)
apa_lm <- apa_print(my_lm)
apa_lm$full_result$Sepal_Length
## [1] "$b = 0.61$, 95\\% CI $[0.48, 0.73]$, $t(146) = 9.77$, $p < .001$"
papaja currently provides methods for the following object classes:
| A-B | D-L | L-S | S-Z | |:-----------------|:--------|:------------------|:-----------------| | afex_aov | default | lsmobj | summary.aovlist | | anova | emmGrid | manova | summary.glht | | anova.lme | glht | merMod | summary.glm | | Anova.mlm | glm | mixed | summary.lm | | aov | htest | papaja_wsci | summary.manova | | aovlist | list | summary_emm | summary.ref.grid | | BFBayesFactor | lm | summary.Anova.mlm | | | BFBayesFactorTop | lme | summary.aov | |
Create tables
apa_table() may be used to produce publication-ready tables in an R
Markdown document. For instance, you might want to report some condition
means (with standard errors).
npk |>
# Summarize data
dplyr::group_by(N, P) |>
dplyr::summarise(
mean = mean(yield)
, se = sd(yield) / sqrt(length(yield))
, .groups = "drop"
) |>
# Label columns
label_variables(
N = "Nitrogen"
, P = "Phosphate"
, mean = "*M*"
, se = "*SE*"
) |>
# Print table
apa_table(caption = "Mean pea yield (with standard errors)")
Table 1. Mean pea yield (with standard errors)
| Nitrogen | Phosphate | M | SE | |:--------:|:---------:|:-----:|:----:| | 0 | 0 | 51.72 | 1.88 | | 0 | 1 | 52.42 | 2.65 | | 1 | 0 | 59.22 | 2.66 | | 1 | 1 | 56.15 | 2.08 |
This is a fairly simple example, but apa_table() may be used to
generate more complex tables.
apa_table(), of course, plays nicely with the output from
apa_print(). Thus, it is possible to conveniently report complete
regression tables, ANOVA tables, or the output from mixed-effects
models.
lm(Sepal.Width ~ Sepal.Length + Petal.Width + Petal.Length, data = iris) |>
apa_print() |>
apa_table(caption = "Iris regression table.")
Table 2. Iris regression table.
| Predictor | b | 95% CI | t | df | p | |:-------------|------:|:----------------:|------:|-----:|:--------| | Intercept | 1.04 | [0.51, 1.58] | 3.85 | 146 | < .001 | | Sepal Length | 0.61 | [0.48, 0.73] | 9.77 | 146 | < .001 | | Petal Width | 0.56 | [0.32, 0.80] | 4.55 | 146 | < .001 | | Petal Length | -0.59 | [-0.71, -0.46] | -9.43 | 146 | < .001 |
Create figures
papaja further provides functions to create publication-ready plots.
For example, you can use apa_barplot(), apa_lineplot(), and
apa_beeplot() (or the general function apa_factorial_plot()) to
visualize the results of factorial study designs:
apa_beeplot(
data = stroop_data
, dv = "response_time"
, id = "id"
, factors = c("congruency", "load")
, ylim = c(0, 800)
, dispersion = wsci # within-subjects confidence intervals
, conf.level = .99
, las = 1
)
<figure>
<img src="README_files/figure-gfm/stroop-plot-1.png"
alt="Response times from a simulated Stroop experiment. Large dots represent condition means, small dots represent individual participants’ mean response time. Error bars represent 99% within-subjects confidence intervals." />
<figcaption aria-hidden="true">Response times from a simulated Stroop
experiment. Large dots represent condition means, small dots represent
individual participants’ mean response time. Error bars represent 99%
within-subjects confidence intervals.</figcaption>
</figure>
If you prefer ggplot2, try theme_apa().
library("ggplot2")
library("ggforce")
p <- ggplot(
stroop_data
, aes(x = congruency, y = response_time, shape = load, fill = load)
) +
geom_violin(alpha = 0.2, color = grey(0.6)) +
geom_sina(color = grey(0.6)) +
stat_summary(position = position_dodge2(0.95), fun.data = mean_cl_normal) +
lims(y = c(0, max(stroop_data$response_time))) +
scale_shape_manual(values = c(21, 22)) +
scale_fill_grey(start = 0.6
