使用森伯斯特绘制工具提示的问题

时间:2019-09-11 23:59:47

标签: r plotly

我正在尝试使用R为Shiny仪表板绘制一个明暗的朝阳图。这是一个简单的图,用于可视化已发生的支出类型及其子类别。该代码运行正常,但是我在工具提示中得到了一些奇怪的附加文本-在所有提示旁边都显示“ Trace 0”。

我已经尝试了工具提示格式的多种变体,但无济于事。我怀疑问题出在数据框的格式上,尽管这是基于Plotly网页中的示例,如果我更改它,该图将不会显示。

这是一些最低限度的可复制代码。

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}'))

运行前面的代码时,我得到一个类似in this image的图形。我希望完全一样,但没有奇怪的“ trace 0”文本。

1 个答案:

答案 0 :(得分:0)

您可以将名称设置为空,

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

或者这也可行,

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