【问题标题】:How to overwrite and edit the hover template of a plotly() 3D object in R如何在 R 中覆盖和编辑 plotly() 3D 对象的悬停模板
【发布时间】:2021-04-26 14:54:01
【问题描述】:

我正在使用plotly() 创建一个 3D 散点图,并希望形成悬停模板。我的数据并不总是相同的列名,但目前这就是我的数据的样子以及我如何构建我的绘图(列名可能不同,因此我将它们保存在一个向量中并重命名我的数据表):

set.seed(123)
dt <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                 spotDE = rnorm(365, 25, 1), windDE = rnorm(365, 10000, 2), 
                 resLoadDE = rnorm(365, 50000, 2), check.names = FALSE)

## Extract the column names of the two selected variables: ##
product1 <- colnames(dt[, 2])
product2 <- colnames(dt[, 3])
product3 <- colnames(dt[, 4])

## Rename the data table: ##
colnames(dt) <- c("date", "prod1", "prod2", "prod3")


## 3D Plot Construction: ##
plot3D <- plot_ly(data = dt, x = ~prod1, y = ~prod2, z =  ~prod3, type = "scatter3d", 
                  mode = "markers", 
                  marker = list(size = 5, 
                                colorscale = list(c(0, 1), c("#A1D99B", "#005A32")),
                                showscale = FALSE)
          ) %>%
          layout(scene = list(xaxis = list(title = product1),
                              yaxis = list(title = product2),
                              zaxis = list(title = product3)), 
                 title = paste('<span style="font-size: 16px;"><b>', product1, "vs.", 
                               product2, "vs.", product3, '</span>'), 
                 margin = list(t = 100))

剧情如下:

现在我需要你的帮助:我怎样才能在hovertemplate而不是xyz中编写相应的产品(在这种情况下:spotDEwindDEresLoadDE) ??

我已经尝试了一些不同的方法,但没有一个有效:

1:这里只有在xyz之后添加。但我想要它。

text = ~paste(product1, ": ", prod1)

2:这里只有在xyz之后添加。但我想要它。

hovertemplate = paste("product1:  %{x}<br>",
                      "%{product2}:  %{y}<br>",
                      "%{product3}:  %{z}<extra></extra>")

【问题讨论】:

    标签: r 3d hover plotly scatter-plot


    【解决方案1】:

    我认为你所拥有的非常接近。我刚刚添加了hoverinfo = 'text',然后将text 添加到plot_ly,引用product1product2product3。您可以使用sprintfformatround 或其他来控制数字输出的更多细节,包括小数位。

    plot_ly(data = dt, x = ~prod1, y = ~prod2, z =  ~prod3, type = "scatter3d", 
                      mode = "markers", 
                      marker = list(size = 5, 
                                    colorscale = list(c(0, 1), c("#A1D99B", "#005A32")),
                                    showscale = FALSE),
            hoverinfo = 'text',
            text = ~paste0(product1, ": ", prod1, "<br>", product2, ": ", prod2, "<br>", product3, ": ", prod3)
    ) %>%
      layout(scene = list(xaxis = list(title = product1),
                          yaxis = list(title = product2),
                          zaxis = list(title = product3)), 
             title = paste('<span style="font-size: 16px;"><b>', product1, "vs.", 
                           product2, "vs.", product3, '</span>'), 
             margin = list(t = 100))
    

    情节

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 2020-09-20
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2021-05-24
      • 1970-01-01
      • 2019-07-22
      相关资源
      最近更新 更多