2016-10-23 2 views
5

"음모"R 패키지를 사용하여 R 그래픽의 이미지를 그려 봅니다. R 배경에 이미지를 추가하십시오.

은 내가 먼저 로컬 컴퓨터에서 이미지가 포함하려고 :

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


    ) 
) 

을하지만 그것을 어떻게 관리하지 않았다. 나는 음모 때문에 분명히 http 나 https 이미지가 필요하다고 생각했다.

첫 번째 질문 : 로컬 파일에서 이미지를 가져올 수 있습니까? (분명히 파이썬으로 가능합니다 : https://plot.ly/python/images/)?

로컬 이미지를 임베딩하는 것이 불가능한 것처럼 Github에 업로드 한 이미지를 가져 오려고합니다. 하지만 둘 다 작동하지 않는 것 같습니다 :

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


    ) 
) 

여기서 어떤 문제가 발생합니까?

나는 도처에서 보았는데 플롯 포럼 (http://community.plot.ly/t/import-a-local-image-in-plot/2476, http://community.plot.ly/t/add-a-background-image/2457)에 질문을 게시했지만 답변을 찾지 못했습니다.

의견이 있으십니까?

+0

본 내용은 https://plot.ly/~as5165/12/#code입니다. R이 아니지만 도움이 될 수 있습니다. 이미지는 base64입니다. 우리가 어떻게 시작하는지 알려주세요. – pssguy

답변

1

변경해야 할 두 가지 작은 것들.

  • URL은 이미지처럼 보였다 뭔가를 지적하지만, 실제로 ?raw=true는 이미지가 좌표가 플롯 외부이었다
  • 이미지를로드 한 후 표시되어 있는지 확인합니다 추가, 전체 GitHub의 페이지를 보여줍니다

htmlwidget을 통해이 코드를 저장해도 CORS 문제로 인해 이미지가 표시되지 않습니다. 두 번째 스 니펫에서 이미지는 base64으로 인코딩되어 플롯에 추가됩니다. RStudio에는 표시되지 않지만 HTML 출력에는 표시되지 않습니다.

아래 코드는 다음 플롯을 생성합니다. 화상 base64 인코딩 용

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

니핏

enter image description here

.

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") 
관련 문제