有没有办法在ggplot2 pdf输出中保留全部边距? [英] Is there a way to keep full margins in ggplot2 pdf output?

查看:60
本文介绍了有没有办法在ggplot2 pdf输出中保留全部边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此答案中

请注意,即使我要求相等的顶部和底部边距,在第一和第二个绘图中也没有底部边距.如果我查看Figure目录中的实际 .pdf 文件,我会发现它们根本不包含任何边距:该文件似乎已被裁剪到的边界框的边缘.页面上的墨水,因此第一个图是(根据Acrobat的文档属性")0.83英寸乘0.83英寸,第二个是0.83英寸乘1.83英寸,最后一个是0.83英寸乘2.83英寸.

我希望得到的是将三个绘图中的每一个垂直放置在我要求的1英寸乘3英寸大小的中心.PDF文件应全部为该大小.

有办法抑制这种作物吗?

解决方案

在浏览了 knitr rmarkdown 源代码一段时间之后,我找到了答案. rmarkdown :: pdf_document 输出函数的参数 fig_crop 默认为 TRUE .将其设置为 FALSE 可以抑制整个文档中图形的裁剪.因此,在此示例中,我要做的就是将标头YAML更改为包括

 输出:pdf_document:fig_crop:否 

,问题就解决了.据我所知,没有块级选项可以更改此设置:所有数字都被裁剪,或者没有.

In this answer https://stackoverflow.com/a/61017301/2554330 I partially answered a problem in resizing subfigures in ggplot2 output using code similar to this:

---
title: "Untitled"
header-includes:
   - \usepackage{subcaption}
output: 
  pdf_document:
    keep_tex: TRUE
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo = FALSE, fig.height=3, fig.width=1,fig.subcap=c("first", "second", "third"),fig.cap="Main"}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) + geom_point()
p1 + theme(plot.margin = unit(c(1,0,1,0),"in") + theme_get()$plot.margin)
p2 + theme(plot.margin = unit(c(1/2,0,1/2,0),"in") + theme_get()$plot.margin)
p3
```

This produces this output:

Notice how there is no bottom margin in the first and second plots, even though I requested equal top and bottom margins. If I look at the actual .pdf files in the figure directory, I can see that they don't contain any margins at all: the file seems to have been cropped to the edge of the bounding box of the ink on the page, so the first plot is (according to Acrobat "Document Properties") 0.83in by 0.83in, the second is 0.83in by 1.83in, and the last one is 0.83in by 2.83in.

What I'd expect to get is to have each of the three plots vertically centred within the 1in by 3in size that I requested. The PDF files should all be that size.

Is there a way to suppress this cropping?

解决方案

After looking through the knitr and rmarkdown source code for a while, I've found the answer. The rmarkdown::pdf_document output function has an argument fig_crop that defaults to TRUE. Setting it to FALSE suppresses cropping of figures in the whole document. So all I need to do in this example is change my header YAML to include

output: 
  pdf_document:
    fig_crop: FALSE

and the problem is solved. As far as I know there is no chunk-level option to change this: all figures are cropped, or none are.

这篇关于有没有办法在ggplot2 pdf输出中保留全部边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆