【问题标题】:Issue with plotly tooltips using sunburst使用 sunburst 的 plotly 工具提示问题
【发布时间】:2020-01-13 19:46:24
【问题描述】:

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

我尝试了 tooltipo 格式的多种变体,但无济于事。我怀疑问题出在数据框的格式上,尽管这是基于 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”文本。

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    你可以设置名字为空,

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

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多