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')
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]
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}'))
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))
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
How to Fix Degree Symbol Not Showing Correctly in R on Linux/Fedora 31
Extracting "((Adj|Noun)+|((Adj|Noun)(Noun-Prep))(Adj|Noun))Noun" from Text (Justeson & Katz, 1995)
Ggplot2: Making Changes to Symbols in The Legend
Summing Multiple Columns in an R Data-Frame Quickly
Filter Dataframe Using Global Variable with The Same Name as Column Name
Is Ifelse Ever Appropriate in a Non-Vectorized Situation and Vice-Versa
How to Align or Center The Bars of a Histogram on The X Axis
How to Get The Intersection Point of Two Vector
How to Install The Fftw3 Package of R in Ubuntu 12.04
Can't Install Any R Packages on Linux Server
Convert to Local Time Zone Using Latitude and Longitude
Inserting Logo into Beamer Presentation Using R Markdown
How to Uninstall R Completely from Os X
How to Define a Function in Dplyr