英文:
Skip 'trace0' from hovertemplate of plotly in R
问题
我有下面的plotly
图,我想跳过在我创建的hovertemplate旁边的"trace0"
值。
library(plotly)
fig <- plot_ly()
fig <- fig %>%
add_trace(
type = 'scatter',
mode = 'lines+markers',
x = c(1,2,3,4,5),
y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
'<br><b>X</b>: %{x}<br>',
'<b>%{text}</b>'),
showlegend = FALSE
)
英文:
I have the plotly
plot below and I want to skip this "trace0"
value next to the hovertemplate I have created.
library(plotly)
fig <- plot_ly()
fig <- fig %>%
add_trace(
type = 'scatter',
mode = 'lines+markers',
x = c(1,2,3,4,5),
y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
'<br><b>X</b>: %{x}<br>',
'<b>%{text}</b>'),
showlegend = FALSE
)
答案1
得分: 1
你应该在hovertemplate下添加<extra></extra>
标签以移除多余的框,就像文档中所描述的那样。你可以使用以下代码:
library(plotly)
fig <- plot_ly()
fig <- fig %>%
add_trace(
type = 'scatter',
mode = 'lines+markers',
x = c(1,2,3,4,5),
y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
'<br><b>X</b>: %{x}<br>',
'<b>%{text}</b>',
'<extra></extra>'),
showlegend = FALSE
)
fig
<!-- -->
<sup>2023-03-07创建,使用reprex v2.0.2</sup>
没有trace0的示例:
英文:
You should add the <extra></extra>
tags to remove the extra box which is your trace0 tag like described here in the docs under hovertemplate. You could use the following code:
library(plotly)
fig <- plot_ly()
fig <- fig %>%
add_trace(
type = 'scatter',
mode = 'lines+markers',
x = c(1,2,3,4,5),
y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
'<br><b>X</b>: %{x}<br>',
'<b>%{text}</b>',
'<extra></extra>'),
showlegend = FALSE
)
fig
<!-- -->
<sup>Created on 2023-03-07 with reprex v2.0.2</sup>
Example without the trace0:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论