Mario M.
Mario M.

Reputation: 872

adjust image size to ggplot2 grid

I need to plot over an image. The problem I have is that my PNG image doesn't adjust to grid:

library(webshot)
library(png)
webshot("http://www.doctormetrics.com/","doctor.png")
img <- readPNG("doctor.png")
dim(img)
x11()
g <- rasterGrob(img, interpolate=TRUE)
qplot(1:2, 1:2, geom="blank") +
  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  geom_point()

enter image description here

Upvotes: 2

Views: 2313

Answers (1)

lukeA
lukeA

Reputation: 54237

You could do

library(webshot)
library(png)
library(ggplot2)
library(grid)
webshot("http://www.doctormetrics.com/",tf<-tempfile(fileext = ".png"))
img <- readPNG(tf)
g <- rasterGrob(img, interpolate=TRUE, height = 1, width = 1)
ggplot() + 
  annotation_custom(g, xmin=1, xmax=2, ymin=1, ymax=2) +
  geom_point(aes(x,y), data.frame(x=1:2, y=1:2)) + 
  coord_fixed(ratio = nrow(img)/ncol(img))

enter image description here

Upvotes: 2

Related Questions