Q:

Rounding off percentages in plotly pie charts

label=c("<25%","25 - 50%",">75%")
values=c(4,2,3)
df=data.frame(label,values)
plot_ly(df, labels = ~label, values = ~values,text=values,textposition="auto", type = 'pie') %>%layout(title = 'Percentage Effort time',showlegend=T,
     xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
     yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

When I run this code, I get a pie chart with percentages and the numbers. How can I obtain percentages that are rounded off to whole numbers instead of decimal points?

A:

You can use textinfo='text' to hide the percent values and provide a custom formatted label with text:
    text = ~paste(round((values / sum(values))*100, 0)),
    textinfo='text',

Complete example:
library(magrittr)
library(plotly)

label=c("<25%","25 - 50%",">75%")
values=c(4,2,3)
df=data.frame(label,values)
plot_ly(df, 
        labels = ~label, 
        values = ~values,
        text = ~paste(round((values / sum(values))*100, 0)),
        #textinfo='none',
        #textinfo='label+value+percent',
        textinfo='text',
        textposition="auto", type = 'pie') %>% layout(title = 'Percentage Effort time', showlegend=T,
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
        )

