【发布时间】: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而不是x、y和z中编写相应的产品(在这种情况下:spotDE, windDE 和 resLoadDE) ??
我已经尝试了一些不同的方法,但没有一个有效:
1:这里只有在x、y和z之后添加。但我想要它。
text = ~paste(product1, ": ", prod1)
2:这里只有在x、y和z之后添加。但我想要它。
hovertemplate = paste("product1: %{x}<br>",
"%{product2}: %{y}<br>",
"%{product3}: %{z}<extra></extra>")
【问题讨论】:
标签: r 3d hover plotly scatter-plot