【问题标题】:Show pictures on Plot in R using for loop使用for循环在R中的Plot上显示图片
【发布时间】:2019-06-02 14:20:56
【问题描述】:

我有一些图像,我想通过定义坐标将这些图像添加到绘图中。

要在绘图上添加单个图像,我有此代码。

require(jpeg)
img<-readJPEG("C:/Users/dell/Desktop/0.jpg")
#now open a plot window with coordinates
plot(1:10,ty="n")
#specify the position of the image through bottom-left and top-right coords
rasterImage(img,2,2,4,4)

但我想在 R 图中显示多个图像

喜欢这个

提前致谢

【问题讨论】:

    标签: r ggplot2 plot data-science


    【解决方案1】:

    也许像下面的代码这样的东西会显示一种如何做问题要求的方法。这不是一个完整的解决方案,因为实际用例图坐标不会相同。

    显而易见的技巧是移动图片

    • 垂直只更改y 坐标。将相同的数量添加到 ybottomytop 以保持比例。
    • 水平只更改x 坐标。将相同的数量添加到 xleftxright 以保持比例。

    在图中的情况下,添加的数量是3

    library(jpeg)
    
    img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
    
    old_par <- par(mar = c(2, 3, 1, 1) + 0.1)
    
    xleft <- 2
    ybottom <- 21
    xright <- 6
    ytop <- 25
    
    plot(seq(0, 60, length.out = 31), 0:30, type = "n")
    for(i in 1:17){
      xleft <- xleft + 3
      xright <- xright + 3
      rasterImage(img, xleft, ybottom, xright, ytop)
    }
    for(i in 1:5){
      ybottom <- ybottom - 3
      ytop <- ytop - 3
      rasterImage(img, xleft, ybottom, xright, ytop)
    }
    for(i in 1:16){
      xleft <- xleft - 3
      xright <- xright - 3
      rasterImage(img, xleft, ybottom, xright, ytop)
    }
    
    par(old_par)
    

    【讨论】:

    • 感谢@Rui Barradas 的及时帮助,我们可以分析模式并构建逻辑,而不是对 x 和 y 值进行头编码,例如 x 增加且 Y 恒定(表示流在 + ve 水平方向,然后继续添加 xleft 值)。
    猜你喜欢
    • 2020-08-23
    • 2018-03-15
    • 1970-01-01
    • 2021-07-09
    • 2011-12-07
    • 1970-01-01
    • 2019-10-08
    • 2017-06-08
    相关资源
    最近更新 更多