在Rmarkdown中调整图形边距

the*_*ide 5 r figure pie-chart r-markdown

我正在尝试调整我的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"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

gri*_*eus 5

除非您指定 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"))
````
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅有关 knitr 块选项的页面


sna*_*aut 5

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

output: 
  pdf_document: 
    fig_crop: no
Run Code Online (Sandbox Code Playgroud)


Fre*_*ehm 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"))
    ```
Run Code Online (Sandbox Code Playgroud)