3

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

Tumaini Kilimba
  • 329
  • 2
  • 12
  • Maybe this question could help? https://stackoverflow.com/questions/54100806/r-markdown-how-do-i-make-text-float-around-figures – Peter Jul 14 '21 at 07:36

1 Answers1

4

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]

enter image description here enter image description here

Peter
  • 11,500
  • 5
  • 21
  • 31
  • 1
    To avoid additional vertical spacing, I would use `\centering` instead of `\begin{center}...\end{center}` – samcarter_is_at_topanswers.xyz Jul 14 '21 at 09:32
  • @samcarter_is_at_topanswers.xyz Thank you for this, and it saves a bit of typing - always a good thing; I've updated the answer. – Peter Jul 14 '21 at 10:23
  • You're welcome! (and very nice answer, btw!) – samcarter_is_at_topanswers.xyz Jul 14 '21 at 10:33
  • 1
    If you want to save typing 3 more characters, you could use `[width=\linewidth]` instead of `[width=0.4\textwidth]`, linewidth will automatically adapt to the width of the wrapfig (won't make any difference to the result) – samcarter_is_at_topanswers.xyz Jul 14 '21 at 10:36
  • The gap could be changed in the same way with `width=.9\linewidth`, but of course the advantage of saving characters to type is then lost :) – samcarter_is_at_topanswers.xyz Jul 14 '21 at 10:59
  • thanks this seems to work a treat... could you please walk me through what is happening here ```{r plot, include=FALSE} png("plot1.png") plot(pressure) dev.off() ``` – Tumaini Kilimba Jul 14 '21 at 15:36
  • I'm not not 100% sure of what is going on in detail. My working understanding is that: png() creates the output file, in this case just the file name with the .png extension into whatever directory you are working in and sends the graphics content to the file. plot() creates the plot, which is the graphic content in the console. dev.off() closes the graphics device, in this case png set by `png()` and passes the plot image to the file. – Peter Jul 14 '21 at 15:54
  • Great answer! After an hour of searching this is the ONLY solution that worked for me. Highly underrated. – albeit2 May 01 '22 at 21:51