【问题标题】:Adjusting figure margins in Rmarkdown在 Rmarkdown 中调整图形边距
【发布时间】:2017-08-28 23:35:50
【问题描述】:

我正在尝试调整我的 Rmarkdown 文档中的饼图,使其跨越页面的宽度,或者至少变得足够宽以适合所有标签。

我已经尝试了所有方法 - 调整 figure.heightfigure.width,使用 par(mar)par(oma) 调整边距,但似乎没有任何效果。要么馅饼本身变小,要么标签被切断更多。我希望馅饼尽可能大,标签清晰可见,但每次它都会渲染小馅饼和小标签。

是否有一种解决方法至少可以使标签不被切断(或可以与相邻图表重叠)?任何建议,将不胜感激。

```{r, figure.align = "center", figure.height = 10, figure.width = 12}

par(mfrow=c(1,3), cex.axis=1.5, cex.lab=1.5) 
par(mar = c(4,2,4,2))
par(oma = c(3, 3, 3, 3))
pie(a, labels = lbls,  font = 2, col = c("tomato", "white"), cex=2)
pie(b, lbls2, font = 2, col = c("tomato", "white"), cex=2) 
mtext(side=3, text="Plan Breakdown: Top 20% of Users")
pie(c, lbls3,  font = 2, col = c("tomato", "white"))

【问题讨论】:

  • 你能提供你正在使用的整个 Rmd 吗?您使用的是哪种输出格式?

标签: r r-markdown pie-chart figure


【解决方案1】:

除非您指定 out.width,否则您的图形大小受文档边距的限制。如果您的图形宽度大于页面的边距,则 R Markdown/knitr 将创建一个指定纵横比的图形,但将其缩小以适应边距。

要解决这个问题,请使用 out.width 设置 pdf 中绘图的宽度和高度。比如:

```{r, fig.align = "center", fig.height = 8, fig.width = 8,
    out.width = "8.5in"}

pie(a, labels = lbls,  font = 2, col = c("tomato", "white"), cex=2)
pie(b, lbls2, font = 2, col = c("tomato", "white"), cex=2) 
mtext(side=3, text="Plan Breakdown: Top 20% of Users")
pie(c, lbls3,  font = 2, col = c("tomato", "white"))
````

更多信息请参见this page on knitr chunk options

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,默认情况下似乎应用了一些裁剪,在 yaml-header 中添加这个对我有用:

    output: 
      pdf_document: 
        fig_crop: no
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用块选项“out.width”。这是我使用的 Rmd 文件。我认为它可以满足您的需求。

          ---
          output: pdf_document
          ---
      
      
          ```{r, out.width='\\textwidth', fig.height = 8, fig.align='center'}
          pie(c(0.57, 0.43), font = 2, col = c("tomato", "white"))
      
          pie(c(0.57, 0.43), font = 2, col = c("blue", "orange"))
          ```
      

      【讨论】:

      • 这对我来说没有任何改变
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2021-12-23
      • 2012-05-16
      • 2021-04-25
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      相关资源
      最近更新 更多