如何在 Plotly 的瀑布图中添加数据标记

How to add Data markers in Waterfall chart in Plotly

我正在尝试使用以下代码绘制瀑布图。我目前面临的唯一问题是数据标记不在正确的位置。我希望数据标记刚好位于每个条的末尾下方。

source('./r_files/flatten_HTML.r')
library("plotly")
 dataset <- data.frame(Category = c("Akash Jain","Ankit Jain","Pankaj Jain","Nitin Pandey","Gopal Pandit","Ramnath Agarwal"),
                      TH =  c(-62,-71,-1010,44,-44,200))
#dataset <- data.frame(Category = Values$Category, TH = Values$TH)
#dataset <- as.data.frame(cbind(Values$Category,Values$TH))
dataset$Category = dataset$Category
dataset$TH = dataset$TH
dataset$SortedCategoryLabel <- sapply(dataset$Category, function(x) gsub(" ", " <br> ", x))
dataset$SortedCategory <- factor(dataset$SortedCategoryLabel, levels = dataset$SortedCategoryLabel)
dataset$id <- seq_along(dataset$TH)
dataset$type <- ifelse(dataset$TH > 0, "in",   "out")
dataset$type <- factor(dataset$type, levels = c("out", "in"))
dataset$end <- cumsum(dataset$TH)
dataset$start <- c(0, head(dataset$end, -1))
Hover_Text <- paste(dataset$Category, "= ", dataset$TH, "<br>")
dataset$colors <- ifelse(dataset$type =="out","red","green")
g <- plot_ly(dataset, x = ~SortedCategory, y = ~start, type = 'bar', marker = list(color = 'rgba(1,1,1, 0.0)'), hoverinfo = 'text') %>%
  add_trace(y = dataset$TH , marker = list(color = ~colors), hoverinfo = "text", text = Hover_Text  ) %>%
  layout(title = '',
         xaxis = list(title = ""),
         yaxis = list(title = ""),
         barmode = 'stack',
         margin = list(l = 50, r = 30, b = 50, t = 20),
         showlegend = FALSE) %>%
  add_annotations(text = dataset$TH,
                  x = dataset$SortedCategoryLabel,
                  y = dataset$end,
                  xref = "dataset$SortedCategoryLabel",
                  yref = "dataset$end",
                  font = list(family = 'Arial',
                              size = 14,
                              color = "black"),
                  showarrow = FALSE)

g

附上瀑布图截图。 因此,对于第一个条,我需要数据标记刚好位于红色条末端的下方。目前它与酒吧重叠。其他人也是如此。

任何帮助将不胜感激。

此致, 阿卡什

您应该在 add_annotations:

中指定 valignheight
vert.align <- c("bottom","top")[as.numeric(dataset$TH>0)+1]

g <- plot_ly(dataset, x = ~SortedCategory, y = ~start, type = 'bar', 
             marker = list(color = 'rgba(1,1,1, 0.0)'), hoverinfo = 'text') %>%
  add_trace(y = dataset$TH , marker = list(color = ~colors), hoverinfo = "text", 
            text = Hover_Text  ) %>%
  layout(title = '',
         xaxis = list(title = ""),
         yaxis = list(title = ""),
         barmode = 'stack',
         margin = list(l = 50, r = 30, b = 50, t = 20),
         showlegend = FALSE) %>%
  add_annotations(text = dataset$TH,
                  x = dataset$SortedCategoryLabel,
                  y = dataset$end,
                  xref = "x",
                  yref = "y",
                  valign=vert.align, height=40,
                  font = list(family = 'Arial',
                              size = 14,
                              color = "black"),
                  showarrow = FALSE)
g