Combining images with data frames in r

This is probably a very simple question, but I am not familiar with the images in R.

I want a PDF file with two related imported images and a data frame, as shown in the image:

Final result

This is the code that I use but does not work.

library(grid)
library(useful)
library(magick)

# Read external images
imageA <- image_read("imageA.jpg") 
imageB <- image_read("imageB.jpg") 

# Create data frame
df <- data.frame(1:3)

# Create PDF
pdf("/Mydocument.pdf", width = 10, height = 20)
grid.newpage() 

# Create matrix layout
pushViewport(viewport(layout = grid.layout(1, 3)))

# Place elements inside grid
print(imageA, vp = vplayout(1, 1)) 
print(imageB, vp = vplayout(1, 2))
print(df, vp = vplayout(1, 3)) 

dev.off()
+4
source share
1 answer

This code provides what you want:

require('magick')

# Read external images
frink = image_read("https://jeroen.imtqy.com/images/frink.png")
logo = image_read("https://www.r-project.org/logo/Rlogo.png")
imgs = c(frink, logo)

# concatenate them left-to-right (use 'stack=T' to do it top-to-bottom)
side_by_side = image_append(imgs, stack=F)

# save the pdf
image_write(side_by_side, path = "just_a_test.pdf", format = "pdf")

which gives this image:

enter image description here

You can scale images vertically with image_scale(imgs, "x960")- changing 960to any desired height in pixels.

magick : R.


: R , : convert img1.png img2.gif +append new_combined.pdf convert. Image Magick, R magick .

R, raster : as.raster(side_by_side)

0

Source: https://habr.com/ru/post/1680918/


All Articles