How to plot 2 jpegs side by side in same output?

Viewed 56

I want to plot 2 jpegs images side by side within the same output.

I have tried to do this two different ways as follows:

library(jpeg)

jpeg("map1.jpeg")
# Set for 2 rows, 1 col
map1 <- par(mfrow=c(1,2),mar=c(9,4,1,0))

jpeg("map2.jpeg")
map2 <-par(mfrow=c(1,2),mar=c(9,4,1,0))

#plot
par(map1, map2)
# Close the device
dev.off()

However, when I attempt this, I only receive this output:

Show in New Window [[1]] NULL

[[2]] NULL

jpeg 2

I have also tried reading the jpegs as rasters.

par(mfrow=c(1,2))
map1 <- raster("map1.jpeg")
map2 <- raster("map2.jpeg")
plot(map1)
plot(map2)

This one shows yellow squares instead of the images...

Does anyone know how to fix this? Thank you.

1 Answers

You can try using {patchwork}. The result you are looking for can be achieved by:

library(patchwork)
map1 + map2
Related