1

Following Sandy Muspratt's answer to the question: "Inserting an image to ggplot outside the chart area", how would you save the output to a jpeg? This would be very helpful for generating reports in R.

Community
  • 1
  • 1
Jonathan Charlton
  • 1,975
  • 6
  • 23
  • 30
  • 1
    Did you try `jpeg("plot_name.jpg"); myplot; dev.off()`? If you are making reports `knitr` may be the way to go. – Tyler Rinker Apr 14 '14 at 13:44
  • I did not. I agree about knitr, based on what I have seen and the little I've read, but I'm trying to get this so that I could drop the report into a slide, a doc, etc. (Also, currently, putting this plot into an R Shiny app; using knitr there is going to be great once I learn it). – Jonathan Charlton Apr 14 '14 at 13:53
  • 1
    I tried my suggestion and it works with the post you linked to. knitr (and other tools like pandoc, slidify, RStudio, pander) can make a slide, a doc etc. except you drop nothing, it drops it for you. – Tyler Rinker Apr 14 '14 at 13:58
  • Could you post your solution. It would be great to see how knitr would apply. I do yet know how to employ knitr. I think it would be great as well for others to see the diversity of the solution. Thank you! – Jonathan Charlton Apr 14 '14 at 14:02

1 Answers1

2

Better to create grobs, arrange them using arrangeGrob and use ggsave:

library(png)
library(grid)
library(ggplot2)
## create image grob
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)
## ggplot2 grob
p = qplot(x=x,y=y,data= data.frame(x=1:10,y=1:10))
library(gridExtra)
## arrange and save 
ggsave('test.png', arrangeGrob(p,g))
agstudy
  • 119,832
  • 17
  • 199
  • 261