R markdown: how to insert space between images - r

I want to add two images side by side in R markdown, but with some space between them.
How can I adjust the space between the images? Thank you!
![](/Users/filename/black.png){width=50%}
![](/Users/filename/gray.png){width=50%}

One option could be using ggdraw() + draw_image from {cowplot}
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r side_by_side, fig.align='center'}
library(cowplot)
ggdraw() +
draw_image("test_logo.png", width = 0.4) +
draw_image("test_animal.jpg", width = 0.4, x = 0.5)
```
Which looks like
Another option could be making white border for the images using image_border from the {magick} package so that even if there is no actual space between them, white border will look like a space.
```{r another_option, warning=FALSE, message=FALSE}
library(magick)
img1 <- image_read("test_logo.png")
bordered_img1 <- image_border(img1, "white", "20x20")
img2 <- image_read("test_animal.jpg")
bordered_img2 <- image_border(img2, "white", "20x20")
image_write(bordered_img1, "img1.png")
image_write(bordered_img2, "img2.jpg")
```
```{r side_img, out.width="50%"}
knitr::include_graphics(c("img1.png", "img2.jpg"))
```
Which looks like,

Related

Displaying the plots stored in a list, suppressing the names and indices from printing into console

I have a few ggplot2 plots stored in a named list, plt_list, and I would like to display the plots in R or Rmarkdown, without the names or list indices (e.g. [[1]]) to be displayed; just the plots. I have tried unname(plt_list), but the indices are printed into the console (or in Rmarkdown document, before each plot). With invisible nothing is displayed. Is there any way to do this in R?
We can use walk from purrr to display in Rmarkdown
---
title: "Title"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r plot_create, echo = FALSE}
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(purrr))
p1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_col()
plt_lst <- list(p1 = p1, p2 = p1, p3 = p1)
```
```{r plots, echo = FALSE, results = 'asis'}
walk(plt_lst, print)
```
-output
If we are trying this in R console, a for loop should also work
for(i in seq_along(plt_lst)) plt_lst[[i]]

knitr cache and ggplot2::last_plot()

Is there a way of make it so that knitr caches stuff so that last_plot() works properly? For example this document:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
library(ggplot2)
```
```{r cars}
ggplot(mtcars, aes(hp, mpg)) +
geom_point()
```
The first value is `r last_plot()$data$mpg[1]`
The first time is generated, it will have one figure and the text "The first value is 21". But the second time, the last inline code will fail.

RMarkdown adjust height to keep bar spacing equal with plots in a loop in a single chunk

In the following RMarkdown, I would like to have each barplot print so that the text size and box width are the same in each plot, and the height adjusts accordingly.
What is happening is that in the plots with few bars the bars are extremely wide, and in the plots with many bars the bars are thin and the labels squished.
---
title: "chunk plot height"
number_sections: yes
output: html_notebook
---
# Load Libraries
\```{r}
library(tidyverse)
library(magrittr)
library(rmarkdown)
\```
# Set Knitr Options
\```{r}
knitr::opts_chunk$set(
echo=TRUE,
dpi=300,
fig.width=12
)
\```
# Plot
\```{r}
for (n in seq(10,50,10))
{
knitr::opts_chunk$set(out.height=n)
data = data.frame(
X=sapply(c(letters, LETTERS)[1:n], function(x) rep(x,3) %>% paste0(collapse=''), USE.NAMES = F),
Y=rnorm(n)
)
plt =
data %>%
ggplot(aes(x=X, y=Y)) +
geom_col(position=position_dodge(width=1, preserve='single')) +
coord_flip()
print(plt)
}
\```

HTML widgets alignment in rmarkdwon

I use the knitr::opts_chunk$set(fig.align = "center") at the beginning of the rmarkdown document to set the alignment of figures. When I output HTML files, the static figures are aligned to the center, but the HTML widgets, such as outputs from leaflet() and ggplotly() have the default alignment (to the left). Is there an option to force the HTML widgets to the center?
EDIT: example given below.
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.align = "center")
library(ggplot2)
library(plotly)
```
```{r static plot}
# This is aligned to center
g <- ggplot(mtcars, aes(mpg, cyl)) +
geom_point()
g
```
```{r html widget}
# html output isn't aligned
p <- ggplotly(g)
p
```
You could resize the plot to the default width of the document and then use some CSS:
---
title: "Untitled"
output: html_document
---
<style>
/* resize the widget container */
.plotly {
width: 100% !important;
}
/* center the widget */
div.svg-container {
margin: auto !important;
}
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.align = "center")
library(ggplot2)
library(plotly)
```
```{r static plot}
# This is aligned to center
g <- ggplot(mtcars, aes(mpg, cyl)) +
geom_point()
g
```
```{r html widget}
p <- ggplotly(g, browser.fill = F) %>%
layout(autosize = F, width = '100%', height = '100%')
p
```

Size of font in ggplot plot changes in relation to plot using knitr

I'm using knitr for the first time, and have a problem concering the font size in ggplot plots. This is an example plot:
d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)
In knitr I have the same plot in a chunk in my R markdown:
---
title: "Untitled"
output: html_document
---
```{r, echo=FALSE}
library(ggplot2)
d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)
```
The plot looks fine in RStudio or when saved with ggsave(). However the numbers in the plot in the resulting knitr html have a much larger font size, in total and relative to the plot size:
In this example it does not matter much, but in my data the numbers start to overlap each other / run out of their cells.
An added complication is that the plot is done by a package, so I can't easily change the size option in the stat_sum call.
Try adjusting fig.height and fig.width:
---
title: "Untitled"
output: html_document
---
```{r, echo=FALSE,fig.height=10,fig.width=10}
library(ggplot2)
d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)
```
If you want don't want the figure to be as large, you can adjust out.height and out.width:
---
title: "Untitled"
output: html_document
---
```{r, echo=FALSE,fig.height=10,fig.width=10,out.height=600,out.width=600}
library(ggplot2)
d <- ggplot(diamonds, aes(x = cut, y = clarity))
d + stat_sum(aes(label=..n..),geom="text",size=8)
```

Resources