The Problem
When using rmarkdown in RStudio, my stargazer(glm())
output gets positioned below the text that I would like it to. It gets positioned in a different spot than the r chunk
is.
The PDF is created perfectly, it's just the position of the stargazer
output that is a problem.
Background
I am trying to create a PDF with lots of text and a couple of stargazer
glm()
output between a few of the paragraphs. When I put more than one stargazer()
output in my rmarkdown file and then "Knit" to PDF, the stargazer()
output gets moved down below the text.
I would like the stargazer output
to get positioned where I put the r chunks
.
I do not have the same problem when using inserting ggplot2()
output in a similar manner.
Failed Attempts
I have tried as many combinations as I know how of positioning my r chunks
arguments. (Just in case)
I have tried every combination of tab vs. spaces, before and after paragraphs/headers/r-chunks/etc. (That was a problem I had once with ggplot2 output)
I have referenced the following StackOverflow Questions:
Reproducible Example
A reproducible example of my work problem:
---
title: "Untitled"
author: "Me"
output: pdf_document
---
```{r setup, echo = FALSE}
library(stargazer)
mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
```
# Heading1
I have tried creating paragraphs like this.
I have also tried creating paragraphs with 2 indents.
## Heading2
Lets try to create a couple of nice tables with stargazer.
```{r attempt1, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)
```
And then we will add some text down here, too.
```{r attempt2, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)
```
And some more text.
```{r attempt3, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)
```
Lets see what happens.
### Heading3
```{r plot_attempt}
boxplot(mtcars$mpg ~ mtcars$cyl)
```
# Second Section
## Second Header
Here are the 3 pages of output:
Here is my sessionInfo:
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stargazer_5.2.1
loaded via a namespace (and not attached):
[1] compiler_3.4.4 backports_1.1.2 magrittr_1.5 rprojroot_1.3-2 htmltools_0.3.6 tools_3.4.4 yaml_2.1.19 Rcpp_0.12.16 stringi_1.1.7 rmarkdown_1.9
[11] knitr_1.20 stringr_1.3.0 digest_0.6.15 evaluate_0.10.1
Thanks
If you can help me, thanks. I do not know much about LaTeX or Pandoc, so I imagine it is some sort of knowledge gap. Feel free just to point me in the right direction, too, if you think you've found a solution.
I appreciate it.
If you set float = FALSE
, you will not have any of the features that come with a floating environment, such as captions (i.e. the title) or labels. Instead, consider setting an unconditional table placement with the float
package. As an example, consider the following document (I use \clearpage
to start the body on page 2 so we can see the adjoining pages on the screenshot):
---
title: "Untitled"
author: "Me"
header-includes:
- \usepackage{lipsum}
output: pdf_document
---
\clearpage
\lipsum[1]
```{r setup, echo = FALSE, include = FALSE}
library(stargazer)
mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
```
Table 1 here.
```{r tab1, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 1")
```
\lipsum[2-3]
Table 2 here.
```{r tab2, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 2")
```
\lipsum[4]
where Table 2 has been bumped to the following page, and the text after Table 2 has been moved up. This is how LaTeX behaves; it does not want to leave too much white space at the bottom of the page. To insist that Table 2 follows a piece of text, you can use the H
specifier (which requires the float
LaTeX package). Here's the same document, but note the table.placement
argument in the tab2
chunk:
---
title: "Untitled"
author: "Me"
header-includes:
- \usepackage{float}
- \usepackage{lipsum}
output: pdf_document
---
\clearpage
\lipsum[1]
```{r setup, echo = FALSE, include = FALSE}
library(stargazer)
mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
```
Table 1 here.
```{r tab1, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 1")
```
\lipsum[2-3]
Table 2 here.
```{r tab2, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 2", table.placement = "H")
```
\lipsum[4]
The table is placed after the text ("Table 2 here"), even at the expense of leaving white space at the bottom of the page. An alternative is \FloatBarrier
from the placeins
package; see https://tex.stackexchange.com/questions/19766/how-to-control-the-position-of-floating-images.
In general, you should leave float (i.e. tables and figures) placements to LaTeX. See https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat for an extensive discussion.