如何在R markdown中使图形浮动/被文本等包围? [英] how to get figure floated / surrounded by text etc. in R markdown?

查看:284
本文介绍了如何在R markdown中使图形浮动/被文本等包围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用LaTeX编写一些报告.由于我的大部分分析都是使用R进行的,因此我切换到Rmarkdown,到目前为止我非常满意,除了一件事,即在pdf输出中包含数字.

I am currently writing some reports with LaTeX. Due to most of my analysis is made with R, I switched to Rmarkdown, which I am very happy with so far, except one thing, namely when it comes to include figures in the pdf output.

我的一些绘图是浮动的,因此它们被文本和其他类型的输出所包围,将我的其他一些图形放在pdf文档中占了整页,尽管它们并不那么大.而且我完全不知道为什么会这样.有谁知道这个问题或解决方案?

Some of my plots are floating, so they are surrounded by text and the other kinds of output, put some other of my figures take a whole page in the pdf document, although they are not that big. And I have absolutely no clue why it is like that. Does anyone know that problem or a solution?

我想永久切换到Rmarkdown,但是如果该图像问题仍然存在,那么我就不能将其用于更严肃的报告,那么表单也很重要.

I would like to switch to Rmarkdown for good, but if that image issue stays like that I can´t use it for more serious reports, where also the form matters.

示例:

---
title: "Multivariate Analysis"
subtitle: "written report"
author: "by XXX"
date: "24.10.2017"
output: 
   pdf_document:
      fig_caption: yes
---

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

...
some more R and Latex stuff which works well, approx. 6 pages
...

These eigenvalues determine the proportion of the total variance explained by the i-th component.

```{r, fig.cap="individual and total proportion of total variance explained by the regarding principal components"}
ggplot() + ...
```

```{r}
options(digits=2)
```

So the first principal component contains already `r eig.val[1]/sum(eig.val)*100`% of the information. 
more text text text text text text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text 

$$
\begin{aligned}
&Y_1=`r eig.vec[1,1]` ...  some latex equations
\end{aligned}
$$

$Y_1$ seems to be text text text text text text text text text text text text text text text text text text text text text text text
text text text text **PAGEBREAK PAGEBREAK** text text text text text text text text text text text text text 

在这种情况下,我在第二部分的页面中断处显示了PAGEBREAK. 在下一页上,我从上方得到了自己的身材,该图仅占用了该页面空间的50%,但该页面上没有其他内容. 在该图之后的下一页上,该文本连续.

In this case I got a pagebreak in the second part of the text where it says PAGEBREAK. On the next page I get my figure from above, which only takes 50% of that pages space but still there´s nothing else on that page. On the next page after the figure the text continous.

那就像是在句子中间的一个刻薄的中断,整个页面之间有一半的空白.

That´s like a harsh break in the middle of a sentence separated by a whole page which is half empty.

为什么会这样?对我来说,哪些图像浮动在文本中,哪些图像不是全部浮动,似乎是随机的,有30-50%的问题与上述相同.

Why is it like that? To me it seems to be random which images are floating in the text and which not in total, 30-50% have that issue as the one above.

有没有人知道如何解决这个问题?每一个提示都是值得欢迎的,我真的很失望...

Does anyone have any kind of idea how to solve that? Every hint is welcome I am really desparated...

我也尝试设置fig.pos ='h',但没有改变任何东西.

I also tried to set fig.pos='h' but didn´t change a thing.

推荐答案

我倾向于按照以下方式整理文件夹

I tend to organize my folder as following

  • 图片
  • 表格
  • 数据

这些文件夹可以放置在markdown(或sweave)脚本所在的文件夹的顶部.它们用于放置图像,由xtable或其他生成的表以及保存的数据和模型.

These folder can be placed on top of the folder where the markdown (or sweave) script is. They are used to place image, tables generated by xtable or other, and saved data and models.

在我的脚本中,我总是有一个初始块,将路径放置到这些目录中,这样,我可以轻松地指向文件夹,如以下示例所示.

In my scripts, I always have an initial chunk where I put the path to those directories, this way I can easily point to the folders as in the following example.

在乳胶序言中,请注意 \ graphicspath {{./images/}} ,它指向您的图片文件夹.

In the latex preamble, note the \graphicspath{{./images/}} which points to your image folder.

在knitr中,使用参数 fig.show ='hide'可以避免直接打印输出.使用 image_dir 参数将图形打印在图像文件夹中.

In knitr, using the argument fig.show='hide' will allow you to avoid directly printing the output. The figure is printed in the image folder using the image_dir argument.

由于使用的是乳胶,因此可以使用pdf()而不是png()保存图片.最后,您可以使用乳胶中的图形环境以所需的方式处理图形.

Since you are using latex, you can use pdf() instead of png() to save the pictures. Finally you use the figure environment in latex to handle your figures the way you want.

\documentclass{article}
\graphicspath{{./images/}}
\usepackage{graphicx}
\title{Test example to show how to handle figure placements}
\date{} % no date
\begin{document}
\maketitle{}

<<include=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
@

<<init, echo = FALSE, results = 'hide'>>=
require(stringr)
# here I set the path to my directories
image_dir <- str_c(getwd(),"/images/",sep="")
@

<<print_figure, echo = FALSE, results = 'hide',fig.show='hide'>>=    
png(file=str_c(image_dir,"random.png"))
plot(rnorm(20))
dev.off()
@

Here is the Figure \ref{test_plot}.
\begin{figure}[htbp]
  \begin{center}
  \includegraphics[width=0.8\textwidth]{random.png}
  \caption{Test plot output.}
  \label{test_plot}
  \end{center}
\end{figure}
\end{document}

这篇关于如何在R markdown中使图形浮动/被文本等包围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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