2

Hi I am trying to plot a pie chart in R plotly for my shiny app where I want to format hover numbers to K or M but couldn't the correct solution for that Here is a reproducible code that I am using, any help would be appreciated

full_data<-data.frame("Name"=c("Q1","Q1","Q2","Q2","Q3","Q3"),"Values"=c(245645,866556,26440,65046,641131,463265))

desc<-full_data%>%group_by(Name)%>%summarise(values=sum(Values))

fig<-plot_ly(desc,labels=~Name,values=~values,type = "pie",
             insidetextfont = list(color = 'white'),
             marker = list(colors = colors,
                           line = list(color = '#FFFFFF', width = 1),hoverformat = ',.0f'))%>%
  layout(title = list(text='<b> Volume(lbs.) </b>',x=0,y=1,titlefont=20),
         font=list(family="Corbel",size=15,color="rgb(33,33,33"),
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         legend=list(font=list(size=18)),
         margin = list(pad = 10,t=60))

fig

Here you can see hover number is 1,112,201 what I want this as 1.1M or if the number is less than a million let's say the number is 125,463 then it should be 125.5K while we hover over the pie chart

Thank you in advance

  • It is not clear to me what the desired output it? could you explain what "format hover numbers to K or M" means? – missuse Oct 12 '20 at 07:15
  • 1
    Hey @missuse I have added the image for your reference – Sumit Kumar Oct 12 '20 at 07:36
  • Perhaps this will help: https://stackoverflow.com/questions/28159936/format-numbers-with-million-m-and-billion-b-suffixes. The answer `scales::label_number_si` seems particulary easy to use. – missuse Oct 12 '20 at 07:44
  • I would have gone with that but the problem for me is in the hovering if numbers need to be displayed other than chart then this would be the perfect solution – Sumit Kumar Oct 12 '20 at 07:50

1 Answers1

2

To achieve a custom hover label you can supply a vector to customdata argument with the desired formatting and define the appropriate hovertemplate.

To get the desired formatting you can use scales::label_number_si:

library(plotly)

desc <- full_data %>% 
  group_by(Name) %>% 
  summarise(values = sum(Values)) %>%
  mutate(lab = scales::label_number_si(accuracy = 0.1)(values))

desc
# A tibble: 3 x 3
  Name   values lab  
  <chr>   <dbl> <chr>
1 Q1    1112201 1.1M 
2 Q2      91486 91.5K
3 Q3    1104396 1.1M 

plot_ly(desc,
        customdata = desc$lab, #pass the lab vector
        labels = ~Name,
        values = ~values,
        type = "pie",
        insidetextfont = list(color = 'white'),
        marker = list(colors = colors,
                      line = list(color = '#FFFFFF', width = 1),
                      hoverformat = ',.0f'),
        hovertemplate = "%{label} <br> %{customdata} <br>  %{percent}") #define hovertemplate

enter image description here

this works too:

plot_ly(desc,
        customdata = ~lab,
        labels = ~Name,
        values = ~values,
        type = "pie",
        insidetextfont = list(color = 'white'),
        marker = list(colors = colors,
                      line = list(color = '#FFFFFF', width = 1),
                      hoverformat = ',.0f'),
        hovertemplate = "%{label} <br> %{customdata} <br>  %{percent}")

To remove the annoying trace 0 in the hover add <extra></extra>:

plot_ly(desc,
        customdata = ~lab,
        labels = ~Name,
        values = ~values,
        type = "pie",
        insidetextfont = list(color = 'white'),
        marker = list(colors = colors,
                      line = list(color = '#FFFFFF', width = 1),
                      hoverformat = ',.0f'),
        hovertemplate = "%{label} <br> %{customdata} <br>  %{percent} <extra></extra>")
missuse
  • 19,056
  • 3
  • 25
  • 47
  • Thanks, missuse that did my job – Sumit Kumar Oct 12 '20 at 09:51
  • In [python customdata can be an array](https://chart-studio.plotly.com/~empet/15366/customdata-for-a-few-plotly-chart-types/#/). However in R a vector is the only possibility. This [workaround](https://github.com/ropensci/plotly/issues/1548) might come in handy, but I could not make it work. – missuse Oct 12 '20 at 10:12