R plotly在背景中添加图像 [英] R plotly add a image in background

查看:36
本文介绍了R plotly在背景中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用plotly"R 包在 R 图形中绘制图像.

我首先尝试包含来自本地计算机的图像:

图书馆(情节)输出文件 <- tempfile(fileext = ".png")png(输出文件)情节(rnorm(200),rnorm(200))dev.off()plot_ly(x = c(1, 2, 3), y = c(1, 2, 3)) %>%布局(图片=列表(列表(源 = 输出文件,外部参照 = "x",yref = "y",x = 1,y = 1,大小 = 2,大小 = 2,尺寸=拉伸",不透明度 = 0.4,层=下面")))

但我没能做到.然后我认为这是因为 plotly 显然需要 http 或 https 图像.

第一个问题:是否可以从本地文件导入图像(显然可以使用 python:

库('plotly')plot_ly(x = c(1, 2, 3), y = c(1, 2, 3), type = 'scatter', mode = 'markers') %>%布局(图片=列表(列表(source = "https://github.com/charlottesirot/elementR/blob/master/inst/www/2.png?raw=true",外部参照 = "x",yref = "y",x = 1,y = 3,大小 = 2,大小 = 2,尺寸=拉伸",不透明度 = 0.4,层=下面")))

<小时>

base64 编码图像的片段.

库('plotly')图书馆('htmlwidgets')库('RCurl')图像文件 <- "/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') %>%布局(图片=列表(列表(source = paste('data:image/png;base64', txt, sep=','),外部参照 = "x",yref = "y",x = 1,y = 3,大小 = 2,大小 = 2,尺寸=拉伸",不透明度 = 0.4,层=下面")))phtmlwidgets::saveWidget(p, "/tmp/plot.html")

I try to use the "plotly" R package to plot an image in an R graphic.

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.

First question: Is it possible to import image from a local file (apparently it is possible with 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. 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.

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
  • 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. In the second snippet the image is base64 encoded and added to the plot. It doesn't show in RStudio but in the HTML output.

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.

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")

这篇关于R plotly在背景中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆