7

I am trying to add an .png image (logo) to the header of my pdf report of graphs created with ggplot and printed to pdf.

I found the following example how to add a image to a ggplot plot. But, I am looking to add the .png image to he header of the pdf which is outside the ggplot area.

#-------------------------------------------------------------------------------
#  Example png file
#-------------------------------------------------------------------------------
library(reshape2)
library(png)
mypngfile = download.file('http://api.altmetric.com/donut/502878_64x64.png', 
                           destfile = 'mypng.png', mode = 'wb')
mypng = readPNG('mypng.png')

#-------------------------------------------------------------------------------
# create example plot using mtcars data frame from ggplot
#-------------------------------------------------------------------------------
library(ggplot2)
p.example = qplot(mpg, wt, data = mtcars) + 
  annotation_raster(mypng, ymin = 4.5, ymax= 5, xmin = 30, xmax = 35)

#-------------------------------------------------------------------------------
# print to pdf file with footnote
#-------------------------------------------------------------------------------
fname = "C:/temp/my report.pdf"
pdf(fname, 10.75, 6.5, onefile=TRUE, paper="a4r")
print(p.example)
dev.off()

...which produces a pdf that looks like this:

enter image description here

But, I would like the image to show up outside the ggplot area. Or more specifically, I want the image to show up in the report header (in upper left) like the following example:

enter image description here

I found the following function that can be used to create a text footnote but wasn't sure how to modify it to insert a .png image.

    makeFootnote <- function(footnoteText= format(Sys.time(), "%d %b %Y"), 
                              size= .4, color= grey(.5))
    {
      require(grid)
      pushViewport(viewport())
      grid.text(label= footnoteText ,
                x = unit(1,"npc") - unit(12, "mm"),
                y = unit(0.1, "mm"),
                just=c("right", "bottom"),
                gp=gpar(cex= size, col=color))
      popViewport()
    }

Any assistance would be greatly appreciated.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
MikeTP
  • 7,716
  • 16
  • 44
  • 57
  • Do you use latex or markdown? If yes, then you may use `wallpaper` package like so: `\ULCornerWallPaper{1}{path/to/logo}; \includegraphics{path/to/ggplot/image}` – tonytonov Oct 30 '14 at 07:29
  • @Tonytonv: Thank you for your reply. To clarify, my image is not a product of ggplot but instead a saved image (i.e. .png file). I will take a look at latex and markdown but thought there should be a simple way to do this with base graphics. Unfortunately, I have gone the ggplot route so I am not familiar with the base graphics. Thanks again for your suggestions. – MikeTP Oct 30 '14 at 15:29
  • Note that PDF specification does not support PNG. Therefore your PNG will be converted to something else, probably JPEG, before being inserted. I would recommend you do this conversion yourself to a format you want, in the quality you want, so that you can be sure of the quality of the final product. I'd recommend JPEG2000. – Alasdair Nov 16 '14 at 18:02

1 Answers1

10

here's a suggestion,

library(ggplot2)
p.example = qplot(mpg, wt, data = mtcars)

library(grid)
library(gtable)
ann <- rasterGrob(mypng, width=unit(1,"cm"), x = unit(0.5,"cm"))
g <- ggplotGrob(p.example)

g <- gtable_add_rows(g, grobHeight(ann), 0)
g <- gtable_add_grob(g, ann, t=1, l=4)
grid.newpage()
grid.draw(g)

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • This is printing the image above the title (when adding `labs(title = ...)`), so it consumes more space on top of the plot. I.e. that's not the 'report header' the OP asks for. – MS Berends Sep 27 '17 at 08:10