0

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?

RAU
  • 13
  • 4
  • could you provide a reproducible example? – CodingBiology Jul 30 '21 at 08:27
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jul 30 '21 at 09:39
  • I will but unfortunately it isn't as easy as I thought to provide an example. I need some time and will be able to provide an example in two weeks. sorry – RAU Jul 30 '21 at 12:00
  • I now replaced the former (useless) code by a reproducible example. I would be happy, if someone has a solution for my problem. – RAU Aug 16 '21 at 12:03

1 Answers1

0

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

stefan
  • 90,330
  • 6
  • 25
  • 51
  • I have a follow-up question: The number format of `'%{percentParent: .1%}'` results in a percent sign without a space between number and "%", e.g. 20,5%. Is there a possibility to insert a space before the "%"? I tried with `hovertemplate = gsub("%", " %",df$hovertemplate)` but that replaced the "%" in my code for the numberformat, resulting in a decimal number, e.g. 0,205882... . – RAU Aug 24 '21 at 09:07