1

The most popular (and simplest) way to adding image to the ggplot2 graph is annotation_custom:

library(ggplot2)
library(png)
library(grid)

img <- readPNG(system.file("img", "Rlogo.png", package="png"), TRUE)
gpp <- rasterGrob(img, interpolate=TRUE)
gpp$width <- unit(1, "npc") 
    gpp$height <- unit(1, "npc")
df <- data.frame(x=seq(1,2,0.01),y=seq(1,2,0.01))
ggplot(df,aes(x=x,y=y)) + 
    annotation_custom(gpp, xmin=1, xmax=2.5, ymin=1, ymax=1.5) +
    geom_point()

In this way, image will be placed over the scale grid.

How to place image under the grid, but with bindings to the coords, not to the borders of the plot?

Stanislav Ivanov
  • 1,634
  • 1
  • 18
  • 21

1 Answers1

2

It's possible in the development version of ggplot2.

How to install it see this answer: https://stackoverflow.com/a/9656182/4265407

Minimal working example:

library(devtools)
dev_mode(on=T)

library(ggplot2)
library(png)
library(grid)

img <- readPNG(system.file("img", "Rlogo.png", package="png"), TRUE)
gpp <- rasterGrob(img, interpolate=TRUE)
gpp$width <- unit(1, "npc") 
gpp$height <- unit(1, "npc")
df <- data.frame(x=seq(1,2,0.01),y=seq(1,2,0.01))
ggplot(df,aes(x=x,y=y)) + 
    annotation_custom(gpp, xmin=1, xmax=2.5, ymin=1, ymax=1.5) +
    geom_point() + theme(panel.ontop=TRUE,
    panel.background = element_rect(colour = NA,fill="transparent"))
Community
  • 1
  • 1
Stanislav Ivanov
  • 1,634
  • 1
  • 18
  • 21