Resizing images in RMarkdown - r

I'm trying to convert a R markdown .Rmd document to .pdf. Unfortunately, the images are too large. Is there any way to change the size of the image? I Can't use html, this is markdown to pdf.

Use this at the beginning of a chunk:
Decimals assigned to fig.height and fig.width are interpreted as inches. Other units of measure also allowed if explicit.
```{r, echo=FALSE, fig.height=2.7, fig.width=9}
#your R code here
```

I found a comfortable solution by the combination of fig.height, fig.width, dpi and out.width.
You can set global parameters at the top by:
knitr::opts_chunk$set(out.width="400px", dpi=120)
You can overwrite these properties in any chunk, just set the parameters you need.
dpi increases the quality image, so you have to adjust by the other parameters.
out.width adjust the size once the image is created.
Decreasing values in fig.height and fig.width will cause the text/numbers to be bigger (same as reducing image window in Rstudio)

There is a simple way to resize Images and still being able to add Captions. Use the following syntax within your RMarkdown code and place the Image's Caption beneath the Image:
<!-- Einbinden von Bildern in RMarkdown -->
\begin{figure}
\centerline{\includegraphics[width=0.5\textwidth]{your_image_name.png}}
\caption{Entitäten zur Persistierung der Special Notifications}
\end{figure}
To scale the image just adapt the width-value from 0.5 to some other percentage-value fitting your needs.
If you don't want to center the images, just remove the \centerline - Command with it's opening and closing brackets {}.

To the best of my knowledge rmarkdown html formats come with Bootstrap. I add the img-responsive with some javascript (at the bottom of my document).
<script>
var d = document.document.getElementsByTagName("img");
d.className += " img-responsive";
</script>

Related

Plotly width larger than content

I'm using Plotly (ggplotly) in my rmarkdown file. The width of the plot is larger than the content, see attached screenshot.
As you can see the legend is outside the content.
Can I fix this using CSS, if so, how?
I just found out that I had set a default fig.width and fig.height in knitr::opts_chunk() which caused this.

How can I make an R Markdown figure chunk hyperlink to the raw image when rendering as HTML?

I would like all of my graphics to link to a full image view once rendered (as the page layout constrains the width and I's like users to be able to zoom in). The chunks are rendered as static images, how do I wrap those in an anchor to the image?
e.g.
```{r "totals", echo=FALSE}
qplot(mtcars$mpg, fill = mtcars$cyl, binwidth = 2)
Renders as
![plot of chunk totals](totals-1.png)
But I want something like
[![plot of chunk totals](totals-1.png)](totals-1.png)
I can wrap the chunk in a markdown link, but that seems a bit clunky and I wondered if there is a way to dynamically code it in the chunk so the image name comes from the chunk name in the same way?

RMarkdown table captions breaks page bottom margin

Whenever I use captions in tables,
like
kable(df[1:10, c(7,1:6)], caption = "This is a caption")
the tables and content in the pdf generated by knitr are pushed below the limits of the bottom margin, thus becoming unreadable. Sometimes entire sections are missing, hidden off margins.
Also, plots positions go crazy: they are printed anywhere but the right place in the pdf.
using results="asis" in chunk options doesn't help.
Using pander causes the same problems.
If I remove all table captions and use some \newpage in the .rmd,
the pdf margins are fine.
Is there a safe way to use table captions?
The pdf in question is here: see page 14 for an entire section missing and table hiding in the bottom margins. Also, the plots are where they want, like if they had proper needs...
github repo
this is kind of a anti-climax,
but as it happens,
this problem was being caused by chunks that printed var values to the document, something like:
```{r}
sampled.values <- sample(1:100, 10)
sampled.values
```
when rendered through render(), this code chunk prints the value of sampled.values and this ruins the pdf pagination.
That's it: all page bottom margins are ok now that I removed all those var calls.

r knit word document plots automatically re-sized

I am trying to add a plot to a word document. I would like the plot to maximize the area available when the page size is set to legal with narrow margins. I can set the fig.width and fig.height but it seems the plots get automatically re-sized to fit the default page size (letter) with normal margins.
Here is a sample .rmd file that produces the same results:
---
title: "plot-resize"
output: word_document
---
Plot with the height set to 3" and the width to 7.5":
```{r, echo = FALSE, fig.height=3, fig.width=7.5, warning=FALSE, message=FALSE}
plot(cars)
```
However when the word document is created the image is automatically
re-sized to 79% of this.
I can re-size the plot in word, but it would be nice to not have to.
Is there a way to set the page size and margins in the .rmd file?
Is there a way to ensure that the plots stay at the specified size even if they do not fit within the margins of the created word document?
You can redo the MS Word template file - see http://rmarkdown.rstudio.com/articles_docx.html - you would have to change your margins to narrow (0.5") in the MS Word template file you are using (Under the Layout ribbon). Then, right click on the figure and select size and position, and then adjust scale height and width to 100%. You would then have to save your template file (and don't forget to close it!) and then add this to your YAML:
title: "plot-resize"
output:
word_document:
reference_docx: mynew_template.docx

Control size of figure in Rstudio presentation

I'm creating a presentation in RStudio (.Rpres). I have a figure that is too large and extends beyond the screen. How can I reduce it?
Too big, need to reduce size
Figure 1
========================================================
![alt text](fig1)
This should work:
<div align="center">
<img src="fig1.jpg" width=800 height=600>
</div>
Simply adjust the width and height as required.
You can use
```{r, echo=FALSE}
knitr::include_graphics(path, dpi = NULL)
```
in a chunk and then set image height and width options in the chunk options. This should work with all output formats.

Resources