简体   繁体   English

R plotly():带有条件内容的悬停模板

[英]R plotly(): hovertemplate with conditional content

I have a sunburst diagram and would like to show some hoverinfo since the labels are quite long.我有一个朝阳图,并且想显示一些悬停信息,因为标签很长。 A small reproducible example:一个可重现的小例子:

library(plotly)
library(dplyr)
library(htmlwidgets)
library(widgetframe)
library(stringr)
library(htmltools)

df <- data.frame(lab = c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"),
            par= c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"),
            ID = c(1,11,12,121,122,13,14,141,15),
            parentID =c(NA,1,1,12,12,1,1,14,1),
            val = c(10, 14, 12, 10, 2, 6, 6, 4, 4))

fig2 <- plot_ly(ids=df$ID,
               labels = df$lab,
               parents = df$parentID,
               values = df$val,
               type = 'sunburst',
               maxdepth=2,
               hovertemplate = paste('%{label}','<br>%{value} EUR<extra></extra>','<br>Anteil an %{parent}','%{percentParent: .1%}'),
                            )
fig2
  

For each element I would like to include the information about the share of parent element in %.对于每个元素,我想包括有关 % 中父元素份额的信息。 This works fine, if I use这工作正常,如果我使用

hovertemplate = paste('%{label}','<br>%{value} EUR<extra></extra>','<br>Anteil an %{parent}','%{percentParent: .1%}')

resulting in hoverinfo of Element with parent , that shows "Anteil an [parent] x %"导致Element 与 parent的悬停信息,显示“Anteil an [parent] x %”

But for the root Element, since there is no parent, I get the following hoverinfo for root element "Anteil an null 100,0 %".但是对于根元素,由于没有父元素,我得到了根元素“Anteil an null 100,0 %”的以下悬停信息。

So for the root element I would like to just show the first part including label and value, but without "Anteil an null 100,0%".因此,对于根元素,我只想显示第一部分,包括 label 和值,但没有“Anteil null 100,0%”。

So far I tried an if else expression.到目前为止,我尝试了一个 if else 表达式。

hovertemplate = if (any(is.na(df[,parent]))) {paste('%{label}','<br>%{value} EUR<extra></extra>')} else {paste('%{label}','<br>%{value} EUR<extra></extra>','<br>Anteil an %{parent}','%{percentParent: .1%}')},

That didn't work.那没有用。

Also, I found a similar topic here , but don't know how to use it.另外,我在这里找到了类似的主题,但不知道如何使用它。

Does anybody have an idea, how to modify the hoverinfo like I need it?有没有人有想法,如何像我需要的那样修改 hoverinfo?

You were on the right track.你走在正确的轨道上。 However, instead of using an if make use of an vectorized ifelse .但是,不要使用if ,而是使用矢量化的ifelse Additionally you could add the hovertemplate as a column to your df which at least in my opinion makes I a bit easier to condition on the parentID and results in a cleaner code:此外,您可以将hovertemplate作为一列添加到您的 df 中,至少在我看来,这使我更容易对parentID进行条件处理并产生更清晰的代码:

library(plotly)
library(dplyr)

df <- df %>% 
  mutate(hovertemplate = ifelse(!is.na(parentID),
                                paste('%{label}','<br>%{value} EUR<extra></extra>',
                                      '<br>Anteil an %{parent}','%{percentParent: .1%}'),
                                paste('%{label}','<br>%{value} EUR<extra></extra>')))
fig2 <- plot_ly(ids=df$ID,
                labels = df$lab,
                parents = df$parentID,
                values = df$val,
                type = 'sunburst',
                maxdepth=2,
                hovertemplate = df$hovertemplate,
)
fig2

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM