R剧情地在背景中添加图片 [英] R plotly add a image in background

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

问题描述

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

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

 库(可打印)outfile<-tempfile(fileext =".png")png(输出文件)情节(rnorm(200),rnorm(200))dev.off()plot_ly(x = c(1,2,3),y = c(1,2,3))%>%布局(图片=列表(列表(源=外档,xref ="x",yref ="y",x = 1y = 1sizex = 2sizey = 2sizing ="stretch",不透明度= 0.4,层=下方"))) 

但是我没有做到这一点.然后我以为是因为显然需要一个http或https图像.

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

  library('plotly')plot_ly(x = c(1,2,3),y = c(1,2,3),type ='scatter',mode ='markers')%&%布局(图片=列表(列表(来源="https://github.com/charlottesirot/elementR/blob/master/inst/www/2.png?raw=true",xref ="x",yref ="y",x = 1y = 3,sizex = 2sizey = 2sizing ="stretch",不透明度= 0.4,layer =在下面"))) 


base64编码图像的代码段.

  library('plotly')库('htmlwidgets')库('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')%&%布局(图片=列表(列表(源=粘贴('data:image/png; base64',txt,sep =','),xref ="x",yref ="y",x = 1y = 3,sizex = 2sizey = 2sizing ="stretch",不透明度= 0.4,layer =在下面")))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剧情地在背景中添加图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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