Labelling the plots with images on graph in ggplot2

前端 未结 1 397
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 17:26

So I have this R script that can produce a scatter plot with labels of each point. Sth like this:

img1<-\"http://blog.gettyimages.com/wp-content/uploads/2         


        
1条回答
  •  甜味超标
    2020-12-16 17:30

    You can use annotation_custom, but it will be a lot of work because each image has to be rendered as a raster object and its location specified. I saved the images as png files to create this example.

    library(ggplot2)
    library(png)
    library(grid)
    
    img1 <- readPNG("c:/test/img1.png")
    
    g1<- rasterGrob(img1, interpolate=TRUE)
    
    
    img2 <- readPNG("c:/test/img2.png")
    g2<- rasterGrob(img2, interpolate=TRUE)
    
    
    plotdata<-data.frame(seq(1:2),seq(11:12))
    ggplot(data=plotdata) +  scale_y_continuous(limits=c(0,4))+ scale_x_continuous(limits=c(0,4))+
      geom_point(data=plotdata, aes(plotdata[,1],plotdata[,2])) +
      annotation_custom(g1,xmin=1, xmax=1.5,ymin=1, ymax=1.5)+
      annotation_custom(g2,xmin=2, xmax=2.5,ymin=2, ymax=2.5) 
    

    enter image description here

    0 讨论(0)
提交回复
热议问题