Search code examples
rif-statementhoverr-plotlysunburst-diagram

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 %"

But for the root Element, since there is no parent, I get the following hoverinfo for root element "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%".

So far I tried an if else expression.

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?


Solution

  • You were on the right track. However, instead of using an if make use of an vectorized 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:

    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
    

    enter image description here