wilsonm2
wilsonm2

Reputation: 591

Can I plot an image to PDF file in R?

Is there a way of loading an image into R and outputting it on pdf?

I know how to plot to pdf and I have tried using the raster and jpeg package but they won't output the image.

The purpose is I am building a PDF pack of charts and want a logo on the page.

Anyone know of another way?

Upvotes: 3

Views: 7444

Answers (4)

Omar Wagih
Omar Wagih

Reputation: 8732

Try the biOps package from CRAN.

# Read image
im = readJpeg('path/to/image')
# Save to pdf
pdf('path/to/pdf')
plot(im)
dev.off();

In my experience, this is a little faster than the other packages i've used

Upvotes: 0

mdsumner
mdsumner

Reputation: 29477

There's nothing wrong with raster for this:

library(raster)
r <- brick(file.path(R.home(), "doc", "html", "logo.jpg"))
pdf("a.pdf")
plotRGB(r)
dev.off()

Note that if you are going to use image() for large rasters you are best to set useRaster = TRUE (raster does this by default for its objects, or use rasterImage() directly) to avoid really large PDF files.

Upvotes: 3

Maxim.K
Maxim.K

Reputation: 4180

I borrow from Matloff's book "The Art of R programming" (p. 63, try Google books):

library(pixmap)
logo <- read.pnm("filename") # the file has to be in PPM format
pdf("path/filename.pdf")
plot(logo)
dev.off()

Alternatively you can try using addlogo() function from the same package, it seems promising for allowing logo coordinates, although I don't have the time to test it thoroughly. You can convert from JPG (or whatever you have) to PPM using one of the online converters.

Also, see this and this thread, both dealing with similar problems. Answers offered there are much more detailed.

Upvotes: 3

Nishanth
Nishanth

Reputation: 7130

Like this -

m = matrix(rnorm(100), 10, 10)
pdf("image.pdf")
image(m)
dev.off()

Upvotes: 0

Related Questions