Adding a picture to plot in R

后端 未结 3 1454
轻奢々
轻奢々 2020-12-14 21:40

I\'m trying to add a picture (jpeg,png doesn\'t care) to a plot which is defined by the layout function. For example:

a<-c(1,2,3,4,5)

b<-c(2,4,8,16,32         


        
3条回答
  •  不思量自难忘°
    2020-12-14 22:10

    Just wanted to offer an alternative solution from the builtin "grid" package called grid.raster

    From what I can tell it acts very much like rasterImage, but takes in normalized units, "npc" --a bonus in my opinion, and preserves aspect ratio unless you set both width and height. For my purposes, i just set either/or and the image seems to scale perfectly.

    library(png)
    library(grid)
    
    x11()
    mypng = readPNG('homer.png')
    image(volcano)
    grid.raster(mypng, x=.3, y=.3, width=.25) # print homer in ll conrner
    grid.raster(mypng, x=.9, y=.7, width=.5) # print bigger homer in ur corner
    
    while(!is.null(dev.list())) Sys.sleep(1)
    

    here's a link

提交回复
热议问题