Как добавить маркеры данных в диаграмму водопада в Plotly - PullRequest
0 голосов
/ 28 апреля 2018

Я пытаюсь построить диаграмму водопада со следующим кодом. Единственная проблема, с которой я сталкиваюсь в настоящее время - это маркер данных, который находится не в правильном месте. Я хочу, чтобы маркер данных был чуть ниже конца каждого бара.

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 голосов
/ 28 апреля 2018

Вы должны указать valign и height внутри add_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

...