fco
fco

Reputation: 13

Issue with plotly tooltips using sunburst

I'm trying to make a plotly sunburst graph for a Shiny dashboard, using R. This is a simple graph to visualize the types of expenses that have occurred and their sub-categories. The code is working fine, but I'm getting some odd additional text in the tooltips - it shows 'Trace 0' next to all of them.

I've tried multiple variations for the tooltipo format, to no avail. I suspect that the issue is with the format of the dataframe, although this is based on an example in Plotly's webpage, and if I change it the graph does not show.

Here is some minimum reproducible code.

library(plotly)

example_df <- structure(
    list(
        type = structure(
            c(6L, 5L, 5L, 5L, 5L, 1L, 1L,
              2L, 2L, 2L, 3L, 3L, 4L, 4L),
            .Label = c("Food", "Fun", "Services", 
                       "Transport", "Expenses", ""),
            class = "factor"
        ),
        subtype = structure(
            c(14L,  13L, 12L, 11L, 10L, 6L, 8L, 2L, 3L, 5L, 4L, 7L, 1L, 9L),
            .Label = c(
                "Car", "Bar", "Drinks", "Entertainment", "Books",
                "Restaurant", "Cleaning", "Market", "Trip", "Food", "Fun",
                "Services", "Transport", "Expenses"),
            class = "factor"
        ),
        cost = c(13969, 5776, 1561, 2822, 3810, 2145, 1665, 1150, 1037, 635, 
                 955, 606, 1334, 4442)
    ),
    row.names = c(NA, -14L),
    class = c("tbl_df", "tbl", "data.frame")
)


plot_ly(example_df,
        labels = ~subtype,
        parents = ~type,
        branchvalues = 'total',
        values = ~cost,
        type = 'sunburst',
        hovertemplate = paste('<b>%{label}</b><br>', '%{value:$,.0f}'))

When I run the previous code, I get a graph like the one in this image. I would like exactly the same but without the weird 'trace 0' text.

Upvotes: 1

Views: 1128

Answers (1)

Sada93
Sada93

Reputation: 2835

You can set the name to be empty,

plot_ly(example_df,
        labels = ~subtype,
        parents = ~type,
        branchvalues = 'total',
        name = "",
        values = ~cost,
        type = 'sunburst',
        hovertemplate = paste('<b>%{label}</b><br>', '%{value:$,.0f}'))

Or this also works,

plot_ly(example_df,
        labels = ~subtype,
        parents = ~type,
        branchvalues = 'total',
        name = "",
        values = ~cost,
        type = 'sunburst',
        hoverinfo = "text",
        hovertext = ~paste0("<b>",subtype,"</b><br>",cost))

Upvotes: 2

Related Questions