Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

How do we plot images at given coordinates in R?

Given 'n' images in a png/jpeg format and 'n' corresponding coordinates in 2 dimensions (x,y): I would like to plot these images at the given coordinates on a single plot. If I find the images to be too big, I would ideally like to plot them as a smaller/scaled version at the given coordinates. How can i achieve such a plot in R?

An example of how such a plot would look is given below:

enter image description here

question from:https://stackoverflow.com/questions/65598585/add-an-image-to-a-plot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
xy <- data.frame(x=runif(10, 0, 100), y=runif(10, 0, 100))

require(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))

thumbnails <- function(x, y, images, width = 0.1*diff(range(x)), 
                       height = 0.1*diff(range(y))){

  images <- replicate(length(x), images, simplify=FALSE)
  stopifnot(length(x) == length(y))

  for (ii in seq_along(x)){
    rasterImage(images[[ii]], xleft=x[ii] - 0.5*width,
                ybottom= y[ii] - 0.5*height,
                xright=x[ii] + 0.5*width, 
                ytop= y[ii] + 0.5*height, interpolate=FALSE)
  }
}

plot(xy, t="n")
thumbnails(xy[,1], xy[,2], img)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...