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

时间:2018-04-28 17:11:09

标签: r r-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

附上瀑布图的截图。 所以对于第一个栏,我需要数据标记位于红色条的末尾。目前它与酒吧重叠。对其他人也一样。

任何帮助都会非常感激。

此致 阿卡什enter image description here

1 个答案:

答案 0 :(得分:1)

您应在valign内指定heightadd_annotations

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

enter image description here