How to hover number formatting to K or M accordingly in R Plotly pie charts and line charts

Viewed 895

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

1 Answers

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>")
Related