Adding Caption to a Stargazer Table

Hi everyone,

I am wondering if anyone has a good way to caption a stargazer table? I am trying to add a caption to tables in an R markdown that I am knitting to a pdf. As far as I can see, the only way to add text beneath the stargazer table is with the "notes" argument, but this seems quite messy if you have a long caption (see reprex). I also tried using the fig.cap = "caption" trick at the top of the chunk containing the table, and this didn't seem to work (I had put fig_caption: yes in the YAML header). Here is a reprex with an example using the built-in mtcars dataset. Any help is greatly appreciated!


library(tidyverse)
library(stargazer)
#> 
#> Please cite as:
#>  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
#>  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer

output <- lm(mpg ~ cyl + disp + wt, data = mtcars)
output2 <- lm(mpg ~ cyl + disp + wt + drat, data = mtcars)

t1 <- stargazer(output, output2, 
                header = FALSE,
                title = "\\textbf{Practice Table}",
                notes = "Notes Could Go Here???????????? This is an example. Notes Could Go Here???????????? This is an example. Notes Could Go Here???????????? This is an example. Notes Could Go Here???????????? This is an example. ",
                notes.align = "l",
                float = FALSE,
                table.placement = "H")
#> 
#> \begin{tabular}{@{\extracolsep{5pt}}lcc} 
#> \\[-1.8ex]\hline 
#> \hline \\[-1.8ex] 
#>  & \multicolumn{2}{c}{\textit{Dependent variable:}} \\ 
#> \cline{2-3} 
#> \\[-1.8ex] & \multicolumn{2}{c}{mpg} \\ 
#> \\[-1.8ex] & (1) & (2)\\ 
#> \hline \\[-1.8ex] 
#>  cyl & $-$1.785$^{***}$ & $-$1.786$^{***}$ \\ 
#>   & (0.607) & (0.635) \\ 
#>   & & \\ 
#>  disp & 0.007 & 0.007 \\ 
#>   & (0.012) & (0.012) \\ 
#>   & & \\ 
#>  wt & $-$3.636$^{***}$ & $-$3.638$^{***}$ \\ 
#>   & (1.040) & (1.102) \\ 
#>   & & \\ 
#>  drat &  & $-$0.010 \\ 
#>   &  & (1.338) \\ 
#>   & & \\ 
#>  Constant & 41.108$^{***}$ & 41.160$^{***}$ \\ 
#>   & (2.842) & (7.304) \\ 
#>   & & \\ 
#> \hline \\[-1.8ex] 
#> Observations & 32 & 32 \\ 
#> R$^{2}$ & 0.833 & 0.833 \\ 
#> Adjusted R$^{2}$ & 0.815 & 0.808 \\ 
#> Residual Std. Error & 2.595 (df = 28) & 2.642 (df = 27) \\ 
#> F Statistic & 46.424$^{***}$ (df = 3; 28) & 33.574$^{***}$ (df = 4; 27) \\ 
#> \hline 
#> \hline \\[-1.8ex] 
#> \textit{Note:}  & \multicolumn{2}{l}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
#>  & \multicolumn{2}{l}{Notes Could Go Here???????????? This is an example. Notes Could Go Here???????????? This is an example. Notes Could Go Here???????????? This is an example. Notes Could Go Here???????????? This is an example. } \\ 
#> \end{tabular}

Created on 2020-04-30 by the reprex package (v0.3.0)

My YAML header in this case was:

...

title: "Title"
author: "Author"
output:
pdf_document:
fig_caption: yes
extra_dependencies: ["rotating"]
header_includes:
\usepackage{float}
\usepackage{dcolumn}
\usepackage{rotating}
\restylefloat{table}

...

Thanks,
Maria

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.