简体   繁体   中英

How to wrap text around charts in a Rmarkdown knit-to-pdf document?

I have an Rmarkdown document which knits to PDF Text not wrapping around chart

My wish is for the white space to the right of the chart be filled by text rather than having all that white space. I have tried the following with the impression that the [h] qualifier will "float" the image with the text wrapping around it. However as you can see from the linked image, this is not the case.

\begin {figure}[h]
\includegraphics[width=8cm] {plot.pdf} 
\end {figure} 
When asked **What are the main problems at the existing Nyakitonto market?** Toilets/bathrooms were the most significant problems with 14% of respondents mentioning them. This is followed by clean water (12%), limited security (11%), vehicular accessibility (10%), health & safety (proximity to busy road) garnered 9%,garbage collection (8.5%) parking facilities for lorries (8%), lack of storage (6%), congestion (4.5%), car parking (3%), and load/offload ramp (1%).

Is there an easy built in way within RStudio Rmarkdown knit-to-PDF documents to wrap text to fill available white space either to the right or left of charts?

Regards

Here is a work around using latex which might be simple even if it is not elegant. Load the wrapfig package in the yaml header.

Ignore the lipsum package, this is to generate text to demonstrate the wrapping.

Updated with @samcarter_is_at_topanswers.xyz suggestion to use \\centering and \\linewidth .

---
title: "wrap text round plot"
output: pdf_document

header-includes:
  - \usepackage{wrapfig}
  - \usepackage{lipsum}

---

Generate a plot; this does not have to be done in the rmarkdown document as you can source an image directly in the latex code.

```{r plot, include=FALSE}

png("plot1.png")

plot(pressure) 

dev.off()

```    

# Wrap text left


\begin{wrapfigure}{r}{0.4\textwidth}
  \centering
    \includegraphics[width=\linewidth]{plot1.png}
  \caption{Plot of pressure against temperature}
\end{wrapfigure}


\lipsum[1-3]

\newpage

# Wrap text right

\begin{wrapfigure}{l}{0.5\textwidth}
  \centering
    \includegraphics[width=0.5\textwidth]{plot1.png}
  \caption{Plot of pressure against temperature}
\end{wrapfigure}

\lipsum[1-3]

在此处输入图片说明 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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