5

I am using the stargazer Package in r to produce summary statistics for latex documents. However, the function produces latex code with the title (caption) on top of the table. Is is there an easy way to put the title below the table?

require(stargazer)
df <- data.frame(a=c(1,2,3),
                 b=c(2,3,5),
                 c=c(8,8,9))

stargazer(df,summary = TRUE, type = "latex",
          title = "summary statistic")

The output of the function is the following:

\begin{table}[!htbp] \centering 
  \caption{summary statistic} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
a & 3 & 2.000 & 1.000 & 1 & 3 \\ 
b & 3 & 3.333 & 1.528 & 2 & 5 \\ 
c & 3 & 8.333 & 0.577 & 8 & 9 \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

Note that the caption is on top of the table. What I would like to have is the following:

\begin{table}[!htbp] \centering 
\begin{tabular}{@{\extracolsep{5pt}}lccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
a & 3 & 2.000 & 1.000 & 1 & 3 \\ 
b & 3 & 3.333 & 1.528 & 2 & 5 \\ 
c & 3 & 8.333 & 0.577 & 8 & 9 \\ 
\hline \\[-1.8ex] 
\end{tabular} 

  \caption{summary statistic} 
  \label{} 

\end{table} 

HTH Andreas

Andreas Dibiasi
  • 261
  • 3
  • 10

2 Answers2

4

I wanted to do the same thing and came to SO to look for an answer. Since there's no existing answer, here's my approach. The basic idea is to write a function that captures and re-creates the stargazer output, moving the caption and label to the bottom:

caption_at_bottom <- function(expr) {
  x <- capture.output(expr)
  cap <- grep("\\\\caption", x)
  lab <- grep("\\\\label", x)
  last <- grep("\\\\end\\{table", x)
  cat(
    paste(
      c(x[-last], x[cap], x[lab], x[last])[-c(cap, lab)]
    , collapse = "\n")
  , "\n")
}

Using your example:

caption_at_bottom(
  stargazer(df, summary = TRUE, type = "latex", header = F,
          title = "summary statistic")
)

# \begin{table}[!htbp] \centering 
# \begin{tabular}{@{\extracolsep{5pt}}lccccc} 
# \\[-1.8ex]\hline 
# \hline \\[-1.8ex] 
# Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
# \hline \\[-1.8ex] 
# a & 3 & 2.000 & 1.000 & 1 & 3 \\ 
# b & 3 & 3.333 & 1.528 & 2 & 5 \\ 
# c & 3 & 8.333 & 0.577 & 8 & 9 \\ 
# \hline \\[-1.8ex] 
# \end{tabular} 
#   \caption{summary statistic} 
#   \label{} 
# \end{table}
Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
0

You want to change the note parameter within the call to stargazer() like

stargazer(df,summary = TRUE, type = "latex",
          title = "summary statistic",
          notes = "put your summary statistic note here"
)
d8aninja
  • 3,233
  • 4
  • 36
  • 60
  • Thanks for your answer. However, this is not what I want. I want the title to appear below the table and not above. In order to achieve that I need the caption and label parameter set below the tabular elements. – Andreas Dibiasi Dec 05 '16 at 07:01