Wrap text around plots in Markdown

2020-02-09 17:57发布

I am reasking this question: How to wrap text around plots in R Markdown?:

'Currently the default for R Markdown in R is to have one line of text in line with the plot but this looks very awkward and I would like to save space by having the text wrap around the plot (plot aligned left, with text wrapping on the right).'

2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-09 18:48

You can use CSS styling to position the elements however you want. However there can be some difficulties and it does require some tweaking. Here is a simple example:

---
title: "Untitled"
author: "Ian Wesley"
date: "April 21, 2017"
output: html_document
---

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

## R Markdown
<div style= "float:right;position: relative; top: -80px;">
```{r pressure, echo=FALSE}
plot(pressure)
```
</div>

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Yields:

enter image description here

查看更多
Juvenile、少年°
3楼-- · 2020-02-09 18:49

It appears there is an easier way now by adding two arguments to the r chunk:

out.width= "65%", out.extra='style="float:right; padding:10px"'

This scales the plot to 65% of the page width and lets text float on the left side with a distance of 10px.

查看更多
登录 后发表回答