简体   繁体   English

R在背景中添加图像

[英]R plotly add a image in background

I try to use the "plotly" R package to plot an image in an R graphic. 我尝试使用“plotly”R包在R图形中绘制图像。

I first tried to include an image from a local computer: 我首先尝试从本地计算机中包含一个图像:

library(plotly)

outfile <- tempfile(fileext = ".png")

png(outfile)
plot(rnorm(200), rnorm(200))
dev.off()

plot_ly(x = c(1, 2, 3), y = c(1, 2, 3)) %>%
  layout(
    images = list(
      list(
        source =  outfile,
        xref = "x",
        yref = "y",
        x = 1,
        y = 1,
        sizex = 2,
        sizey = 2,
        sizing = "stretch",
        opacity = 0.4,
        layer = "below"
      )


    )
  )

But I did not manage to do it. 但我没有设法做到这一点。 I then thought that was because plotly apparently requires an http or https image. 然后我认为那是因为情节显然需要http或https图像。

First question: Is it possible to import image from a local file (apparently it is possible with python: https://plot.ly/python/images/ )? 第一个问题:是否可以从本地文件导入图像(显然可以使用python: https//plot.ly/python/images/ )?

As it seems to be impossible to embed a local image, I try to import an image that I had upload on my Github. 由于似乎无法嵌入本地图像,我尝试导入我在Github上传的图像。 But it seems not to work neither: 但它似乎既不工作:

library(plotly)

plot_ly(x = c(1, 2, 3), y = c(1, 2, 3)) %>%
  layout(
    images = list(
      list(
        source =  "https://github.com/charlottesirot/elementR/blob/master/inst/www/2.png",
        xref = "x",
        yref = "y",
        x = 1,
        y = 1,
        sizex = 2,
        sizey = 2,
        sizing = "stretch",
        opacity = 0.4,
        layer = "below"
      )


    )
  )

What is the problem here? 这里有什么问题?

I have looked everywhere, posted questions on plotly forum ( http://community.plot.ly/t/import-a-local-image-in-plot/2476 , http://community.plot.ly/t/add-a-background-image/2457 ) but I did not find my answers. 我已经到处看,贴在plotly论坛问题( http://community.plot.ly/t/import-a-local-image-in-plot/2476http://community.plot.ly/t/add -a-background-image / 2457 )但我找不到答案。

Do you have any idea? 你有什么主意吗?

Two small things which needed to be changed. 需要改变的两件小事。

  • The URL pointed to something which looked like an image but actually shows the whole GitHub page, appending ?raw=true makes sure that only the image is shown URL指向看起来像图像但实际显示整个GitHub页面的内容,附加?raw=true确保只显示图像
  • After loading the image the coordinates were outside the plot 加载图像后,坐标位于图之外

Saving this code via htmlwidget still does not show the image because of some CORS issue. 由于某些CORS问题,通过htmlwidget保存此代码仍然无法显示图像。 In the second snippet the image is base64 encoded and added to the plot. 在第二个片段中,图像是base64编码并添加到绘图中。 It doesn't show in RStudio but in the HTML output. 它不会显示在RStudio中,而是显示在HTML输出中。

The code below produces the following plot. 下面的代码生成以下图表。

在此输入图像描述

library('plotly')

plot_ly(x = c(1, 2, 3), y = c(1, 2, 3), type = 'scatter', mode = 'markers') %>%
  layout(
    images = list(
      list(
        source =  "https://github.com/charlottesirot/elementR/blob/master/inst/www/2.png?raw=true",
        xref = "x",
        yref = "y",
        x = 1,
        y = 3,
        sizex = 2,
        sizey = 2,
        sizing = "stretch",
        opacity = 0.4,
        layer = "below"
      )
    )
  )

Snippet for base64 encoded image. base64编码图像的片段。

library('plotly')
library('htmlwidgets')
library('RCurl')

image_file <- "/temp/2.png"
txt <- RCurl::base64Encode(readBin(image_file, "raw", file.info(image_file)[1, "size"]), "txt")


p <- plot_ly(x = c(1, 2, 3), y = c(1, 2, 3), type = 'scatter', mode = 'markers') %>%
  layout(
    images = list(
      list(
        source =  paste('data:image/png;base64', txt, sep=','),
        xref = "x",
        yref = "y",
        x = 1,
        y = 3,
        sizex = 2,
        sizey = 2,
        sizing = "stretch",
        opacity = 0.4,
        layer = "below"
      )
    )
  )
p
htmlwidgets::saveWidget(p, "/tmp/plot.html")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM