【问题标题】:How to add Data markers in Waterfall chart in Plotly如何在 Plotly 的瀑布图中添加数据标记
【发布时间】:2018-04-28 17:11:08
【问题描述】:

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

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

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

任何帮助将不胜感激。

问候, 阿卡什

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    您应该在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
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      相关资源
      最近更新 更多