在Rmarkdown中调整图表边距

10

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

我已经尝试了一切 - 调整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吗?你使用哪种输出格式? - Fred Boehm
4个回答

6

如果您没有指定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"))
````

请参阅此页面关于knitr代码块选项的更多信息。


5

我也遇到了相同的问题,似乎默认情况下应用了一些裁剪,将这个代码添加到yaml头文件中对我有用:

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"))
    ```

这对我的情况没有任何影响。 - snaut

0
你可以去文件<首选项<编辑器然后滚动到:"文本编辑器的最大宽度"将其设置为"1200px"。这将增加Markdown文档的宽度。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接