【问题标题】:How to display the total value at the top of the stacked bar chart in R如何在R中的堆积条形图顶部显示总值
【发布时间】:2020-08-14 13:25:44
【问题描述】:

我有一个数据框,从中在 R 中绘制堆积条形图

myDF <- data.frame("group" = c("A","B","C","A","B","C"),
                    "date" = c("25-May","25-May","25-May","01-Jun","01-Jun","01-Jun"),
                     "hours" = c(42,48,51,32,25,48),
                      stringsAsFactors = FALSE)

p <- plot_ly(myDF, x = myDF$date,
        y = myDF$group,
        type = 'bar',
        name = myDF$group,
        text = myDF$Hours,
        color = myDF$group,
        colors = brewer.pal(length(unique(myDF$group)),
                            "Paired"))%>%
  layout(barmode = 'stack',hoverlabel = list(bgcolor= 'white') ,bargap = 0.5) %>%
  layout(xaxis = list(categoryorder = 'array',
                      categoryarray = myDF$date), showlegend = F)

我有另一个数据框,其中我每周的总小时数如下:

totalHours <- data.frame(
                          "date" = c("25-May","01-Jun"),
                           "hours" = c(141,105))

我正在尝试像这样在栏的顶部显示堆栈的总和

annotations <- list()
for (i in 1:length(totalHours$hours)) {
  annotations[[i]] <- list(x = totalHours$date[[i]],
                          text = totalHours$hours[[i]],
                          yanchor='bottom',
                          showarrow = FALSE)
}

在绘图布局中添加注释

p <- p %>% layout(annotations = annotations)

我将标签放在中心而不是显示在顶部。我不清楚代码中哪里有遗漏。谁能为此提供解决方案?

提前致谢!!

【问题讨论】:

    标签: r annotations plotly


    【解决方案1】:

    你可以试试这个。这是标签位置的问题:

    library(plotly)
    library(RColorBrewer)
    #Data
    myDF <- data.frame("group" = c("A","B","C","A","B","C"),
                       "date" = c("25-May","25-May","25-May","01-Jun","01-Jun","01-Jun"),
                       "hours" = c(42,48,51,32,25,48),
                       stringsAsFactors = FALSE)
    #Plot
    p <- plot_ly(myDF, x = myDF$date,
                 y = myDF$group,
                 type = 'bar',
                 name = myDF$group,
                 text = myDF$Hours,
                 color = myDF$group,
                 colors = brewer.pal(length(unique(myDF$group)),
                                     "Paired"))%>%
      layout(barmode = 'stack',hoverlabel = list(bgcolor= 'white') ,bargap = 0.5) %>%
      layout(xaxis = list(categoryorder = 'array',
                          categoryarray = myDF$date), showlegend = F)
    #Data2
    totalHours <- data.frame(
      "date" = c("25-May","01-Jun"),
      "hours" = c(141,105),stringsAsFactors = F)
    
    #Create global coordinate
    labs <- myDF %>% group_by(group) %>% mutate(Tot = sum(hours),Prop=hours/Tot) %>% 
      ungroup() %>% summarise(Val=sum(Prop))
    #Annotate
    annotations <- list()
    for (i in 1:length(totalHours$hours)) {
      annotations[[i]] <- list(x = totalHours$date[[i]],
                               y = labs$Val,
                               text = totalHours$hours[[i]],
                               yanchor='bottom',
                               showarrow = FALSE)
    }
    #Final plot
    p <- p %>% layout(annotations = annotations)
    

    输出:

    【讨论】:

    • 全局坐标到底有什么用?当我尝试使用其他数据时,标签会随机显示
    • @NevedhaAyyanar 对不起,如果我让你感到困惑。它是关于基于所有组的比例的累积值。您可以使用 groupdate 等分组变量并计算 y 值。
    • 是否有可能使用单个数据框而不是使用 2 个数据框来实现这一目标?
    • @NevedhaAyyanar 您可以围绕相同的myDF 进行计算,并将值存储在新列中,并在注释中仔细选择y 的值。
    • 我不太清楚在注释中修复y 的值。它在图的中心显示标签,而不是在顶部显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多