【问题标题】:R Plotly: issue xref/x layout when x axis is POSIXct formatR Plotly:当x轴为POSIXct格式时发出外部参照/x布局
【发布时间】:2020-09-21 12:16:01
【问题描述】:

我已经开始使用 Plotly 库,当 x 轴为 POSIXct 格式时,我无法通过布局添加图像。

这是一个可重现的例子:

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

这是结果:

正如你在图中看到的,r 标志没有显示出来。有人可以帮忙吗?

谢谢!

【问题讨论】:

标签: r time-series plotly posixct


【解决方案1】:

在this answer 之后,真正的问题是关于图像的大小。 Plotly 以毫秒为单位确定时间轴上图像的大小,所以有一个图像,但它真的非常小 xref = "x" 和 sizex = 1。您还可以将xref = "paper" 设置为相对于绘图布局。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2020-10-06
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多