Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R Markdown using R code chunks, can two images be placed on one the same line with one aligned left and one aligned right?

Tags:

r

r-markdown

I'm creating a pdf using R Markdown with a header that has two logos, one aligned left and one right. Because I'm using R code chunks to resize the images, the result has the second in a new line. Is it possible to have two images on the same line but with different alignment?

This is the current code, resulting in images on separate lines:

---
title: ''
output: pdf_document
---

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


```{r logo, echo=FALSE,out.width="200px",out.height="200px",fig.align='left'}
knitr::include_graphics("pic1.jpg")
```
```{r logo2, echo=FALSE, out.width="200px",out.height="200px",fig.align='right'}
knitr::include_graphics("pic2.jpg")
```
like image 927
user127126 Avatar asked Jan 29 '26 06:01

user127126


1 Answers

Here I've assumed we're laying out two general images, rather than plots. If your images are actually plots you've created, then you can lay them out as a single image for display using gridExtra::grid.arrange for grid graphics or par(mfrow=c(1,2)) for base graphics and thereby avoid the complications of laying out two separate images.

I'm not sure if there's a "natural" way to left justify the left-hand image and right-justify the right-hand image. As a hack, you could add a blank "spacer" image to separate the two "real" images and set the widths of each image to match paper-width minus 2*margin-width.

Here's an example where the paper is assumed to be 8.5" wide and the right and left margins are each 1":

---
output: pdf_document
geometry: margin=1in
---

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

# Create a blank image to use for spacing
spacer = ggplot() + theme_void() + ggsave("spacer.png")
```

```{r, out.width=c('2.75in','1in','2.75in')}
include_graphics(c("Rplot59.png","spacer.png", "Rplot60.png"))
```

And here's what the document looks like:

enter image description here

like image 119
eipi10 Avatar answered Jan 30 '26 23:01

eipi10