Search code examples
rr-plotly

How to adjust a long text string inside a hover label in plotly pie?


Imk trying to fit a long text string inside a hover label on my pie chart. I would like the text to automatically adjust to the label so its properly shown.

In addition, i would like to remove the Trace 0 enter image description here

A sample dataset:

df.prescripciones.3.direccionadas.grafica = structure(list(IPS = c("UNIDAD ESPECIALIZADA EN ALERGIA Y ENFERMEDAD RESPIRATORIA SAS", 
"HOSPITAL INTERNACIONAL DE COLOMBIA", "FUNDACION CARDIOVASCULAR DE COLOMBIA - INSTITUTO CARDIOVASCULAR", 
"Hospital Virtual", "INSTITUTO DE MEDICINA AMBULATORIA Y PREVENTIVA IMAP", 
"MEDYSER IPS", "FUNDACION OFTALMOLOGICA DE SDER FOSCAL", "ESE HOSPITAL PSIQUIÁTRICO SAN CAMILO", 
"Total"), `2021-06` = c(113844, 113844, 65234911, 113844, 
113844, 113844, 113844, 113844, 113844), Total = c(113844, 
113844, 65234911, 113844, 113844, 113844, 113844, 113844, 
113844)), row.names = c(NA, -9L), class = c("tabyl", "tbl_df", 
"tbl", "data.frame"), core = structure(list(IPS = c("UNIDAD ESPECIALIZADA EN ALERGIA Y ENFERMEDAD RESPIRATORIA SAS", 
"HOSPITAL INTERNACIONAL DE COLOMBIA", "FUNDACION CARDIOVASCULAR DE COLOMBIA - INSTITUTO CARDIOVASCULAR", 
"Hospital Virtual", "INSTITUTO DE MEDICINA AMBULATORIA Y PREVENTIVA IMAP", 
"MEDYSER IPS", "FUNDACION OFTALMOLOGICA DE SDER FOSCAL", "ESE HOSPITAL PSIQUIÁTRICO SAN CAMILO"
), `2021-06` = c(113844, 189728293, 65234911, 37038984, 5506387, 
1198435, 1372392, 383629)), row.names = c(NA, -8L), class = "data.frame"), tabyl_type = "two_way", totals = c("col", 
"row"))

Part of the server code that creates the pie chart:

paired1 <- brewer.pal(n = 10, name = "Paired")

  output$plot4 <- renderPlotly(
  plot4 <- plot_ly( df.prescripciones.3.direccionadas.grafica ,
                    labels = ~Profesional, 
                    values = ~Total,
                    type = 'pie',
                    marker = list(colors = paired1),
                    texttemplate = ~ paste ("<br>",
                                            "%{percent} <br>"),
                    hovertemplate = ~ paste (  Profesional,
                                               " <br>",IPS," <br>",
                                               "%{percent} <br>",
                                               "$","%{value} <br>" )  ) %>%
    layout( title = list  ( text = "Top 10 Profesionales"),
            font = list (size = 11) ,
            hoverlabel = list(font=list(size=11) ) ) %>% 
    layout(showlegend = F) %>%
    layout(legend = list(x = -0.5, y = 0.5)) %>% 
    layout(autosize = T,  margin = list(l = 25,
                                        r = 65,
                                        b = 40,
                                        t = 60))  %>%
    layout(plot_bgcolor  = "rgba(0, 0, 0, 0)",
           paper_bgcolor = "rgba(0, 0, 0, 0)"
    ) 
)

The final pie that is generated:

enter image description here


Solution

  • There doesn't seem to be an argument for defining the textlength. But you can split long text inside the hovertemplate and therefore get a break every x characters.

    An example for plain text can be found in this post - gsub is used. In the code below the break will be added every 45 characters.

    To remove the "Trace 0" you add <extra></extra> at the end of the hovertemplate.

    Code

     plot_ly( df.prescripciones.3.direccionadas.grafica ,
                        labels = ~IPS, 
                        values = ~Total,
                        type = 'pie',
                        marker = list(colors = paired1),
                        texttemplate = ~ paste ("<br>",
                                                "%{percent} <br>"),
    # added gsub and <extra></extra>
                        hovertemplate = ~ paste (gsub('(.{1,45})(\\s|$)', '\\1\n', IPS),
                                                   "%{percent} <br>",
                                                   "$","%{value} <br> <extra></extra>" )) %>%
        layout( title = list  ( text = "Top 10 Profesionales"),
                font = list (size = 11) ,
                hoverlabel = list(font=list(size=11) ) ) %>% 
        layout(showlegend = F) %>%
        layout(legend = list(x = -0.5, y = 0.5)) %>% 
        layout(autosize = T,  margin = list(l = 25,
                                            r = 65,
                                            b = 40,
                                            t = 60))  %>%
        layout(plot_bgcolor  = "rgba(0, 0, 0, 0)",
               paper_bgcolor = "rgba(0, 0, 0, 0)"
        ) 
    

    Post
    enter image description here