Printing an image to to a PDF


I created a 20 pages PDF with graphs and charts in R.

In one of the pages I need to incorporate a .jpeg snipped image from a document.

How would you do that?

Tried a few options that I found online such as plot(), print(), image().

pdf(file="file.pdf"),paper="USr", width=10, height = 7)
print(".jpeg loaction")

The code above didnt really work.


Compose in RMarkdown or Quarto. Those will allow you to mix text, inline and chunk code output including plots and local or linked images. These can be output to pdf, html or pdf. (Or Word :-1:)

the whole infrastructure have been created to use PDFs instead of Rmarkdown. There is a 99.999% chance I wont be changing anything. Hence somehow showing an image into a PDF would be much more preferable.

Here's a "somehow." Can't say I'd recommend it.

# adapted from
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)

ggplot(,aes(1:10, 1:10, geom="blank")) +
  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +

Created on 2022-12-22 by the reprex package (v2.0.1)

1 Like

That actually works! Thanks!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.