简体   繁体   English

控制 knitr 中两个并排图的对齐方式

[英]Control alignment of two side-by-side plots in knitr

I cannot figure out how to arrange two side-by-side plots as explained in the knitr graphics manual page 2 ( http://yihui.name/knitr/demo/graphics/ ).我不知道如何按照 knitr 图形手册第 2 页( http://yihui.name/knitr/demo/graphics/ )中的说明排列两个并排的图。 I use the following MWE and the output is below.我使用以下 MWE,输出如下。 I would like them to be aligned as shown in the manual for the two cars plots (also on page 2 of the manual).我希望它们按照两辆汽车图的手册中所示对齐(也在手册的第 2 页)。 The pdf is generated in RStudio (Knit to PDF). pdf 是在 RStudio(Knit to PDF)中生成的。

---
title: "Untitled"
output: pdf_document
---

## R Markdown

```{r,echo=FALSE,out.width='.49\\linewidth', fig.width=3, fig.height=3}

barplot(1:4)
barplot(4:7)

```

在此处输入图像描述

To center two plots you can add fig.align='center' to your chunk options.要使两个图居中,您可以将fig.align='center'添加到块选项中。 If it produces one plot above the other add also fig.show='hold' .如果它产生一个高于另一个的图,还添加fig.show='hold' The result should be two centered graphs.结果应该是两个居中的图。 结果

So your final chunk option should look something like:所以你的最终块选项应该是这样的:

{r,echo=FALSE, out.width='.49\\linewidth', fig.width=3, fig.height=3,fig.show='hold',fig.align='center'}

An alternative which worked for me: save the plots as files, then put the picture markdown on the same line (from here ).对我有用的替代方法:将绘图保存为文件,然后将图片降价放在同一行上(来自此处)。

## Show images
![](file1.pdf) ![](file2.pdf)

You can save ggplot plot objects with ggsave.您可以使用 ggsave 保存 ggplot 绘图对象。

Base plots are a bit more complicated, see for example here .基本地块有点复杂,请参见此处的示例。

I'm putting this answer here because it helped me.我把这个答案放在这里是因为它对我有帮助。 Whether it's better for you or not depends on your situation.它是否对您更好取决于您的情况。

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

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