简体   繁体   English

如何在 {xaringan} 演示文稿中缩小 {stargazer} 表?

[英]How to make {stargazer} table smaller in {xaringan} presentation?

I'm making my first Ninja Presentation and included a stargazer table that runs off the slide (too long).我正在制作我的第一个 Ninja 演示文稿,并包括一张从幻灯片上滑下来的观星台(太长了)。 Any suggestions?有什么建议?

Reproducible table to be put in {xaringan}.可复制的表要放在 {xaringan} 中。

library(tidyverse)
library(stargazer)
df<-tibble(y=sample(1:100,500,replace=TRUE),x=sample(1:5,500,replace=TRUE),x2=sample(1:3,500,replace=TRUE),x3=sample(1:15,500,replace=TRUE),x4=sample(LETTERS[1:5],500,replace=TRUE))
stargazer(lm(df$y~df$x+df$x2+df$x3+df$x4),type="html")

along with html output for those who just want to plop that in:以及那些只想将其放入的人的 html 输出:

<table style="text-align:center"><tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td><em>Dependent variable:</em></td></tr>
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>y</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">x</td><td>-1.175</td></tr>
<tr><td style="text-align:left"></td><td>(0.902)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x2</td><td>-0.742</td></tr>
<tr><td style="text-align:left"></td><td>(1.580)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x3</td><td>0.028</td></tr>
<tr><td style="text-align:left"></td><td>(0.309)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4B</td><td>0.660</td></tr>
<tr><td style="text-align:left"></td><td>(4.181)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4C</td><td>-2.830</td></tr>
<tr><td style="text-align:left"></td><td>(4.283)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4D</td><td>2.581</td></tr>
<tr><td style="text-align:left"></td><td>(4.221)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4E</td><td>1.637</td></tr>
<tr><td style="text-align:left"></td><td>(4.100)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>55.593<sup>***</sup></td></tr>
<tr><td style="text-align:left"></td><td>(5.977)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>500</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>0.008</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>-0.006</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>29.048 (df = 492)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>0.575 (df = 7; 492)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>
</table>

There are many ways to make an HTML table smaller.有很多方法可以使 HTML 表格变小。 I suggest two things here:我在这里建议两件事:

  1. set single.row = T in stargazer::stargazer() .stargazer::stargazer()设置single.row = T This gives you coefficient estimates and corresponding standard errors on the same line and hence is more space-efficient.这为您提供了同一行上的系数估计和相应的标准误差,因此更节省空间。

  2. Reduce the font size.减小字体大小。 For this you need some CSS, eg table { font-size: 12px; }为此,您需要一些 CSS,例如table { font-size: 12px; } table { font-size: 12px; } . table { font-size: 12px; } . You may embed this in a code chunk:您可以将其嵌入到代码块中:

     ```{css, echo = F} table { font-size: 12px; } ```

    Note that this will change the font size for all tables in your presentation.请注意,这将更改演示文稿中所有表格的字体大小。 A more general approach would be to define a class for a specific table style and than assigning that class to the slide:更通用的方法是为特定表格样式定义一个类,而不是将该类分配给幻灯片:

     ```{css, echo = F} .regression table { font-size: 12px; } ``` --- class: regression # Hello World < code producing the table >

Using the default Xaringan template, the result looks like this:使用默认的 Xaringan 模板,结果如下所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM