Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

latex

I have some models that have long titles to be fully explanatory. It would be helpful to have them print their descriptors or titles on two lines. This reads the line break character, but the resulting latex output doesn't recognize it.

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

Thanks.

like image 810
spindoctor Avatar asked Mar 06 '18 18:03

spindoctor


2 Answers

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')

enter image description here

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] 

enter image description here

like image 87
dww Avatar answered Nov 14 '22 23:11

dww


For multi-line column labels in html, the word you are looking for is:

<br>

For example, I have r-code that reads a CSV, modifies the labels with line breaks, and writes out as html like this:

filename <- "table1.csv"
pubTable <- read.table(file=filename,header = TRUE, sep = ",")
labels = c("Period", "2D and 3D <br>Image <br>Analysis"," Document <br>Processing"," 
Biometric <br>Identification ","Image <br>Databases","Video <br>Analysis","Biomedical 
<br>and <br>Biological")

library(stargazer)
stargazer(pubTable[],type = "html", rownames = FALSE, 
summary=FALSE,out="Table1.html",covariate.labels = labels)

The resulting table looks like this 1: enter image description here

1 Adopted from Conte, Donatello, et al. "Thirty years of graph matching in pattern recognition." International journal of pattern recognition and artificial intelligence 18.03 (2004): 265-298.

like image 45
Scott Button Avatar answered Nov 14 '22 23:11

Scott Button