plotly / plotly/plotly.R

Stacked Barchart with total amount of bars text

未关闭
#1,775 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

Problem: I have a stacked barchart. Above each of the stacked bars I want to have a total number (i.e. the sum of the two bars - here: total). I tried to take usage of add_text but it did not work out as expected. Any suggestions what to change?

Remark: I do not want to cast the data.table object. The target frame on which the visualizations shall be made is new_frame.

Many thanks


Brief description of the problem

library(data.table)
library(plotly)

Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.table(Animals, SF_Zoo, LA_Zoo)

new_frame <- data.table::melt(data, id.vars='Animals')

new_frame[, total := sum(value), by = Animals]

plot_ly(new_frame, x = ~Animals,
        y = ~value,
        type = 'bar',
        name = ~variable,
        text = ~paste(variable, value, total),
        textposition = 'auto',
        # insidetextfont = list(size = 10, color = 'black'),
        hovertext = ~paste(
            "<b>Hover text works IN/OUT</b><br>"),
        hoverinfo = 'text',
        hoverlabel = list(align = "left"),
        showlegend = TRUE) %>%
    add_text(
        visible = "legendonly",
        x= ~Animals,
        y = ~total,
        # showlegend = TRUE,
        name = "Titel",
        cliponaxis = FALSE,
        textfont = list(size = 10)
    )%>%
    layout(yaxis = list(title = 'Count'), barmode = 'stack')

image

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从使用 data.table::melt、plot_ly 和 add_text 的可复现 R 示例开始,然后检查堆叠条形图 trace 如何定位总计标签。完成标准是在不对 data.table 对象进行 cast 的情况下,在 new_frame 上每个堆叠条形图的上方显示总和。

由索引模型根据 Issue 内容生成。

评估

技术栈
plotly, r
领域
data-visualization
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。