Getting Stargazer Column Labels to Print on Two or Three Lines

Getting Stargazer Column labels to print on two or three lines?

One option: You can insert latex codes for newline (\\) and table column alignment (&). Note that each \ needs to be "escaped" in R with another \.

stargazer(mod, column.labels='my models\\\\ & need long titles')

Sample Image

Another way is to multirow. This could be easier for more complex tables with different length headings on each column. You will need to add \usepackage{multirow} to your document preamble.

stargazer(mod, column.labels='\\multirow{2}{4 cm}{my models need long titles}')

You will also need to post-edit the latex output from stargazer to insert some extra lines (using \\) below the variable headings so that the rest of the table gets moved down also (as in the exceprt below):

& \multirow{2}{4 cm}{my models need long titles} \\ 
\\ % note the extra line inserted here before the horizontal rule
\hline \\[-1.8ex]

Sample Image

Stargazer column on multiple lines *with multiple models*?

Ok, my apologies, I was really close, I should have looked a little harder before posting. The key was 1) to use \shortstack instead of \mbox and 2) not to escape the { characters.

var1<-rnorm(100)
var2<-rnorm(100)
df<-data.frame(var1, var2)
mod<-lm(var1~var2)
library(stargazer)
stargazer(mod, mod, column.labels=c('\\shortstack{my models \\\\ need long titles}',
'\\shortstack{my models \\\\ need long titles}'))

Sample Image

Generate multiple column names for multiple models using `stargazer()`

I don't know how to achieve this in stargazer, but it easy to do using the modelsummary and kableExtra packages (disclaimer: I am the modelsummary maintainer):

library(kableExtra)
library(modelsummary)

mod<-list(
"(1)" = mod,
"(2)" = mod,
"(3)" = mod,
"(4)" = mod
)

modelsummary(mod, output = "latex") %>%
add_header_above(c(" ", "Age>25" = 1, "Age<=25" = 1, "Educ>12" = 1, "Educ<=25" = 1)) %>%
add_header_above(c(" ", "Gender=Male" = 2, "Gender=Female" = 2)) %>%
add_header_above(c(" ", "Dependent variable:" = 4))

Sample Image

How to change column names in stargazer when printing data frames?

A bit hacky, but the main idea here is to gsub out the relevant row in the stargazer output with our desired Latex code:

# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)

out <- capture.output(
stargazer(df, summary = F,
notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
)

# Changing names
vars = c("x" = "$X$", "y" = "$Y$\\\\textsuperscript{1}")
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
# \begin{table}[!htbp] \centering
# \caption{}
# \label{}
# \begin{tabular}{@{\extracolsep{5pt}} ccc}
# \\[-1.8ex]\hline
# \hline \\[-1.8ex]
# & $X$ & $Y$\textsuperscript{1} \\
# \hline \\[-1.8ex]
# 1 & $1$ & $6$ \\
# 2 & $2$ & $7$ \\
# 3 & $3$ & $8$ \\
# 4 & $4$ & $9$ \\
# 5 & $5$ & $10$ \\
# \hline \\[-1.8ex]
# \multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \\
# \end{tabular}
# \end{table}

When using argument align = T, try

vars = sprintf("\\\\multicolumn\\{1\\}\\{c\\}\\{%s\\}", 
c("$X$", "$Y$\\\\textsuperscript{1}"))
names(vars) <- sprintf("\\\\multicolumn\\{1\\}\\{c\\}\\{%s\\}", names(df))
cat(sep = "\n",
gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)


Related Topics



Leave a reply



Submit