【问题标题】:How to show calculated values on top of stacked bar chart in r如何在r中的堆积条形图顶部显示计算值
【发布时间】:2017-04-20 19:17:12
【问题描述】:

使用下面的代码,我有一个如图所示的堆积条形图。

myDF <- structure(list(Group = structure(1:3, .Label = c("2017-04-02", 
"2017-04-09", "2017-04-16"), class = "factor"), Passive = c(4, 
1, 0), Promoter = c(12, 1, 4), Detractors = c(0, 0, 0)), .Names = c("Group", 
"Passive", "Promoter", "Detractors"), row.names = c(NA, -3L), class = "data.frame")


x <- list(
  title = ""
)
y <- list(
  title = "Count"
)

p <- plot_ly(myDF, x = ~Group)

if ("Detractors" %in% colnames(myDF[-1])){
  p <- add_trace(p, y = ~`Detractors`, name = 'Detractors', type = 'bar',
                 marker = list(color = '#D52728')) #red
}
if ("Passive" %in% colnames(myDF[-1])){
  p <- add_trace(p, y = ~`Passive`, name = 'Passive', type = 'bar', 
                 marker = list(color = '#1F78B4')) #orange
}
if ("Promoter" %in% colnames(myDF[-1])){
  p <- add_trace(p, y = ~`Promoter`, name = 'Promoter', type = 'bar', 
                 marker = list(color = '#2BA02D')) #green
}
p <- layout(p, xaxis = x, yaxis = y, barmode = 'stack', legend = list(orientation = 'h'), showlegend=T)

p

我想在每个条形图的顶部显示净推荐值。我用于计算 NPS 的公式是: (Number of Promoters — Number of Detractors) / (Number of Respondents) x 100。 所以对于第一个酒吧,它将是((12 - 0)/16) * 100 = 75。我想在第一个栏的顶部显示NPS: 75。同样,对于第二条和第三条,顶部的数字将分别为 (1-0)/2*100 = 50(4-0)/4*100 = 100。我将在接下来的几周内获得更多数据,因此未来每周的数据都会有一个条形图。有没有办法在条形图的顶部显示这个计算值?

【问题讨论】:

    标签: r plotly stacked-chart


    【解决方案1】:

    您可以将annotations 添加到您的layout 中,其中x 值是您的日期,y 值是堆叠值,文本是Net Promoter Score,例如

    df = data.frame(x = c('A', 'B', 'C', 'D'),
                    y = c(1,3,2,4),
                    calculated_values = c(20,30,10,50)
    )
    annotations <- list()
    for (i in 1:length(df$calculated_values)) {
      annotations[[i]] <- list(x = df$x[[i]],
                               y = df$y[[i]],
                               text = df$calculated_values[[i]],
                               yanchor='bottom',
                               showarrow = FALSE)
    
    }
    plot_ly(df,
            x = ~x, 
            y = ~y, 
            type = 'bar') %>% 
      layout(annotations = annotations)
    

    或者对于这个特定的示例,将代码的最后两行替换为:

    annotations <- list()
    for (row in rownames(myDF)) {
      annotations[[as.integer(row)]] = list(x = as.character(myDF[row,]$Group),
                                    y = sum(myDF[row,][-1]),
                                    text = ((myDF[row,][[3]] - myDF[row,][[4]]) / sum(myDF[row,][-1])) * 100,
                                    yanchor='bottom',
                                    showarrow = FALSE)
    
    }
    p <- layout(p, xaxis = x, yaxis = y, barmode = 'stack', legend = list(orientation = 'h'), showlegend=T, 
                annotations = annotations)
    
    p
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2022-08-19
      • 1970-01-01
      相关资源
      最近更新 更多