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

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

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 &quot;trace0&quot; value next to the hovertemplate I have created.

library(plotly)

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

答案1

得分: 1

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

library(plotly)

fig &lt;- plot_ly() 
fig &lt;- fig %&gt;%
  add_trace(
    type = &#39;scatter&#39;,
    mode = &#39;lines+markers&#39;,
    x = c(1,2,3,4,5),
    y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
    text = c(&quot;Text A&quot;, &quot;Text B&quot;, &quot;Text C&quot;, &quot;Text D&quot;, &quot;Text E&quot;),
    hovertemplate = paste(&#39;&lt;i&gt;Price&lt;/i&gt;: $%{y:.2f}&#39;,
                          &#39;&lt;br&gt;&lt;b&gt;X&lt;/b&gt;: %{x}&lt;br&gt;&#39;,
                          &#39;&lt;b&gt;%{text}&lt;/b&gt;&#39;,
                          &#39;&lt;extra&gt;&lt;/extra&gt;&#39;),
    showlegend = FALSE
  ) 
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:

library(plotly)

fig &lt;- plot_ly() 
fig &lt;- fig %&gt;%
  add_trace(
    type = &#39;scatter&#39;,
    mode = &#39;lines+markers&#39;,
    x = c(1,2,3,4,5),
    y = c(2.02825,1.63728,6.83839,4.8485,4.73463),
    text = c(&quot;Text A&quot;, &quot;Text B&quot;, &quot;Text C&quot;, &quot;Text D&quot;, &quot;Text E&quot;),
    hovertemplate = paste(&#39;&lt;i&gt;Price&lt;/i&gt;: $%{y:.2f}&#39;,
                          &#39;&lt;br&gt;&lt;b&gt;X&lt;/b&gt;: %{x}&lt;br&gt;&#39;,
                          &#39;&lt;b&gt;%{text}&lt;/b&gt;&#39;,
                          &#39;&lt;extra&gt;&lt;/extra&gt;&#39;),
    showlegend = FALSE
  ) 
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:

确定