2016-10-23 6 views
5

Ich versuche, das "Plotly" R-Paket zu verwenden, um ein Bild in einer R-Grafik zu plotten.R plotly ein Bild im Hintergrund hinzufügen

Ich habe versucht, zuerst ein Bild von einem lokalen Computer enthalten:

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


    ) 
) 

Aber ich schaffe nicht, es zu tun. Ich dachte das war, weil plotly anscheinend ein HTTP- oder HTTPS-Bild benötigt.

Erste Frage: Ist es möglich, Bild von einer lokalen Datei zu importieren (anscheinend ist es mit Python möglich: https://plot.ly/python/images/)?

Da es scheinbar unmöglich ist, ein lokales Bild einzubetten, versuche ich ein Bild zu importieren, das ich auf meinen Github hochgeladen habe. Aber es scheint auch nicht zu funktionieren:

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


    ) 
) 

Was ist das Problem hier?

Ich habe überall gesucht, Fragen auf Plotly Forum (http://community.plot.ly/t/import-a-local-image-in-plot/2476, http://community.plot.ly/t/add-a-background-image/2457) gestellt, aber ich habe meine Antworten nicht gefunden.

Haben Sie eine Idee?

+0

Haben Sie diesen https://plot.ly/~as5165/12/#code gesehen? Nicht R aber kann helfen. Bild ist base64. Lass uns wissen, wie es dir geht – pssguy

Antwort

1

Zwei kleine Dinge, die geändert werden mussten.

  • Die URL deutete auf etwas, das wie ein Bild aussah, aber tatsächlich zeigt die ganze Seite GitHub, Anfügen ?raw=true stellt sicher, dass nur das Bild außerhalb des Grundstücks
  • Nach dem Laden des Bildes die Koordinaten waren
  • gezeigt

Wenn Sie diesen Code über htmlwidget speichern, wird das Bild wegen des Problems CORS immer noch nicht angezeigt. In dem zweiten Ausschnitt ist das Bild base64 codiert und dem Plot hinzugefügt. Es wird nicht in RStudio angezeigt, sondern in der HTML-Ausgabe.

Der folgende Code erzeugt die folgende Grafik.

enter image description here

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

Schnipsel für Bild codiert 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")