跳过在 R 中的 Plotly 的 hovertemplate 中的 ‘trace0’。

huangapple go评论125阅读模式
英文:

Skip 'trace0' from hovertemplate of plotly in R

问题

我有下面的plotly图,我想跳过在我创建的hovertemplate旁边的"trace0"值。

  1. library(plotly)
  2. fig <- plot_ly()
  3. fig <- fig %>%
  4. add_trace(
  5. type = 'scatter',
  6. mode = 'lines+markers',
  7. x = c(1,2,3,4,5),
  8. y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
  9. text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
  10. hovertemplate = paste('<i>Price</i>: $%{y:.2f}',
  11. '<br><b>X</b>: %{x}<br>',
  12. '<b>%{text}</b>'),
  13. showlegend = FALSE
  14. )
英文:

I have the plotly plot below and I want to skip this &quot;trace0&quot; value next to the hovertemplate I have created.

  1. library(plotly)
  2. fig &lt;- plot_ly()
  3. fig &lt;- fig %&gt;%
  4. add_trace(
  5. type = &#39;scatter&#39;,
  6. mode = &#39;lines+markers&#39;,
  7. x = c(1,2,3,4,5),
  8. y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
  9. text = c(&quot;Text A&quot;, &quot;Text B&quot;, &quot;Text C&quot;, &quot;Text D&quot;, &quot;Text E&quot;),
  10. hovertemplate = paste(&#39;&lt;i&gt;Price&lt;/i&gt;: $%{y:.2f}&#39;,
  11. &#39;&lt;br&gt;&lt;b&gt;X&lt;/b&gt;: %{x}&lt;br&gt;&#39;,
  12. &#39;&lt;b&gt;%{text}&lt;/b&gt;&#39;),
  13. showlegend = FALSE
  14. )

答案1

得分: 1

你应该在hovertemplate下添加&lt;extra&gt;&lt;/extra&gt;标签以移除多余的框,就像文档中所描述的那样。你可以使用以下代码:

  1. library(plotly)
  2. fig &lt;- plot_ly()
  3. fig &lt;- fig %&gt;%
  4. add_trace(
  5. type = &#39;scatter&#39;,
  6. mode = &#39;lines+markers&#39;,
  7. x = c(1,2,3,4,5),
  8. y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
  9. text = c(&quot;Text A&quot;, &quot;Text B&quot;, &quot;Text C&quot;, &quot;Text D&quot;, &quot;Text E&quot;),
  10. hovertemplate = paste(&#39;&lt;i&gt;Price&lt;/i&gt;: $%{y:.2f}&#39;,
  11. &#39;&lt;br&gt;&lt;b&gt;X&lt;/b&gt;: %{x}&lt;br&gt;&#39;,
  12. &#39;&lt;b&gt;%{text}&lt;/b&gt;&#39;,
  13. &#39;&lt;extra&gt;&lt;/extra&gt;&#39;),
  14. showlegend = FALSE
  15. )
  16. fig

跳过在 R 中的 Plotly 的 hovertemplate 中的 ‘trace0’。<!-- -->

<sup>2023-03-07创建,使用reprex v2.0.2</sup>


没有trace0的示例:

跳过在 R 中的 Plotly 的 hovertemplate 中的 ‘trace0’。

英文:

You should add the &lt;extra&gt;&lt;/extra&gt; 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:

  1. library(plotly)
  2. fig &lt;- plot_ly()
  3. fig &lt;- fig %&gt;%
  4. add_trace(
  5. type = &#39;scatter&#39;,
  6. mode = &#39;lines+markers&#39;,
  7. x = c(1,2,3,4,5),
  8. y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
  9. text = c(&quot;Text A&quot;, &quot;Text B&quot;, &quot;Text C&quot;, &quot;Text D&quot;, &quot;Text E&quot;),
  10. hovertemplate = paste(&#39;&lt;i&gt;Price&lt;/i&gt;: $%{y:.2f}&#39;,
  11. &#39;&lt;br&gt;&lt;b&gt;X&lt;/b&gt;: %{x}&lt;br&gt;&#39;,
  12. &#39;&lt;b&gt;%{text}&lt;/b&gt;&#39;,
  13. &#39;&lt;extra&gt;&lt;/extra&gt;&#39;),
  14. showlegend = FALSE
  15. )
  16. fig

跳过在 R 中的 Plotly 的 hovertemplate 中的 ‘trace0’。<!-- -->

<sup>Created on 2023-03-07 with reprex v2.0.2</sup>


Example without the trace0:

跳过在 R 中的 Plotly 的 hovertemplate 中的 ‘trace0’。

huangapple
  • 本文由 发表于 2023年3月7日 18:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定