Create white space between png images created in leaflet placed side by side using rMarkdown

Hello, I am able to create 2 png images (maps) in leaflet and call using plot2 and plot3 having assigned the images in the working directory to these objects. I had to use leaflet to do this due to issues I have had with adding basemaps in other packages such as ggplot and tmap.
I can put these images side by side using R Markdown:

knitr::include_graphics(c(plot3, plot2))

I want to insert a sliver of white space between the maps so it is clear they are separate. How can I do this? I have tried using the following so I can call it in R Markdown but I get a blank png image:

png(filename = "context-and-catchment-map.png", width = 2500, height = 1600, res = 300)
op1 <- par(mfrow = c(1,2))
op2 <- par(mar = c(5,4,5,2)+0.1)
m <- leaflet(x_G84041) %>% addTiles() %>% addPolygons(fillColor = ~pal(Patients),
fillOpacity = 0.7, color = "#000000", weight = 1, opacity = 1) %>%
addAwesomeMarkers(lng = coord$long, lat = coord$lat, icon = icon.fa) %>%
addLegend(pal = pal, values = ~Patients)

par(op2)
op3 <- par(mar = c(5,5,4,2)+0.1)
n <- leaflet(ccg_br) %>% addTiles() %>% addPolygons(color = "#000000", weight = 2, fillOpacity = 0.1) %>%
addPolygons(data = x_G84041, color = "#000000", weight = 1, fillColor = "#2E8B57", fillOpacity = 1) %>%
addAwesomeMarkers(lng = coord$long, lat = coord$lat)

par(op3)
par(op1)
dev.off()

Any help much appreciated.
Thanks

Hi @Jonw!

This might be coming at the question a bit sideways, but since you already have PNG images, how about just adding borders, courtesy of ImageMagick and the magick package? A very simple example on a dark background to make the effect obvious:

---
title: "Adding borders to PNGs`"
output: html_document
---

```{css, echo=FALSE}
body {background-color: #777; color: #fff;}
```

```{r}
library(magick)

img <- image_read("https://i.imgur.com/esbT74s.png")

img_with_border <- image_border(img, "white", "20x20")

image_write(img, "img.png")
image_write(img_with_border, "img_with_border.png")
```

# Borderless
```{r}
knitr::include_graphics(c("img.png", "img.png"))
```

# With border
```{r}
knitr::include_graphics(c("img_with_border.png", "img_with_border.png"))
```

You could get more elaborate with the borders, too. The magick compositing functions can do an awful lot when you put them together!

3 Likes

Thank you jcblum,
That is perfect. I have spent the last 3 days trying to sort that out and you have given me a very simple solution both to this and putting the image in the corner. I can now choose which to do.
Regards
JonW

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.