Two little things that needed to be changed.
- The URL pointed to what looked like an image, but actually showed the entire GitHub page, adding
?raw=true , make sure that only the image is displayed - After loading the image, the coordinates were off the chart
Saving this code with htmlwidget still does not display the image due to a CORS error. In the second fragment, the base64 image is encoded and added to the plot. It is not displayed in RStudio, but in HTML output.
The code below shows the following graph.

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