1

I have started to use Plotly library and I´m not be able to add an image via layout whe x axis is POSIXct format.

Here a reproducible example:

date <- seq.POSIXt(from = as.POSIXct("2015-02-21 15:00", tz = "GMT"),   
                   to = as.POSIXct("2015-02-21 23:00", tz = "GMT"),
                   by = 3600)

set.seed(135)
temp <- runif(9, -5, 20)

df <- as.data.frame(date)

df[,'temp'] <- temp

p <- plot_ly(df, x = ~date, y= ~temp, type = "scatter", mode = "line")

p %>% layout(
  images = list(
    source = "https://images.plot.ly/language-icons/api-home/r-logo.png",
    xref = "x",
    yref = "paper",
    x = as.POSIXct("2015-02-21 18:00", tz = "GMT"),
    y = 0.5,
    sizex = 1,
    sizey = 1,
    opacity = 1
  
  )
)

This is the result:

enter image description here

As you can see in the figure the r logo is not shown. Someone could help with this?

Thanks!

2

1 Answer 1

1

Following this answer, the real issue is about the size of the image. Plotly determines the size of an image on a time axis in milliseconds, so there is an image but it's really really small with xref = "x" and sizex = 1. You could have also set xref = "paper" to be relative to the plot layout.

library(plotly)
date <- seq.POSIXt(from = as.POSIXct("2015-02-21 15:00", tz = "GMT"),
                   to = as.POSIXct("2015-02-21 23:00", tz = "GMT"),
                   by =3600)

set.seed(135)
temp <- runif(9, -5, 20)

df <- as.data.frame(date)

df[, 'temp'] <- temp

p <- plotly::plot_ly(df, x = ~date, y= ~temp, type = "scatter", mode = "line")
p %>% plotly::layout(
  images = list(
    source = "https://images.plot.ly/language-icons/api-home/r-logo.png",
    xref = "x",
    yref = "paper",
    x = as.POSIXct("2015-02-21 18:00", tz = "GMT"),
    y = 1,
    # 7.2 million milliseconds (2 hours) wide 
    sizex = 7.2e6,
    sizey = 1,
    opacity = 1,
    # centered the image
    xanchor = "center"
    
  )
)

![enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.