Как иметь фиксированный размер в воронкообразной диаграмме, но динамический c текст при наведении? - PullRequest
2 голосов
/ 13 января 2020

Я пытаюсь получить воронкообразную диаграмму, используя R.

Проблема в том, что числа искажены и они не будут иметь одинакового представления. Тем не менее, мы хотим показать поток. Итак, как я могу создать воронковую диаграмму в R, где размер воронки фиксирован, но текст имеет динамический характер c и поступает из столбца данных.

См. Пример исходного кода: https://plot.ly/r/funnel-charts/

requiredPackages <- c("plotly")

ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg))
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

ipak(requiredPackages)

p <- plot_ly(
  type = "funnelarea",
  values = c(5, 4, 3, 2, 1),
  text = c("The 1st","The 2nd", "The 3rd", "The 4th", "The 5th"),
  marker = list(colors = c("deepskyblue", "lightsalmon", "tan", "teal", "silver"),
                line = list(color = c("wheat", "wheat", "blue", "wheat", "wheat"), 
  width = c(0, 1, 5, 0, 4))),
  textfont = list(family = "Old Standard TT, serif", size = 13, color = "black"),
  opacity = 0.65)

p

Результат

enter image description here

1 Ответ

2 голосов
/ 21 января 2020

Я хочу показать диаграмму воронки, как указано выше; но при наведении я хочу показать другой текст. Туннель предназначен только для представления фигур, но при наведении указываются фактические значения.

Исходя из приведенного выше комментария к OP, я предполагаю, что они хотят, чтобы hoverinfo выбрала его текст из этого вектора значений c(5,4,3,2,1).

Первый график / решение работает в этом отношении:

library(plotly)

plot_ly(
  type = "funnelarea",
  values = c(5, 4, 3, 2, 1),
  text = c("The 1st","The 2nd", "The 3rd", "The 4th", "The 5th"),
  marker = list(colors = c("deepskyblue", "lightsalmon", "tan", "teal", "silver"),
                line = list(color = c("wheat", "wheat", "blue", "wheat", "wheat"),
  width = c(0, 1, 5, 0, 4))),
  textfont = list(family = "Old Standard TT, serif", size = 13, color = "black"),
  opacity = 0.65,
  hovertemplate = '%{value}<extra></extra>')
                          <img src="https://i.stack.imgur.com/EeqRK.png" width="330" height="330">

Возможно добавить больше текста / значений. Ниже приведен пример этого. Вы можете прочитать больше здесь: https://plot.ly/r/hover-text-and-formatting/

plot_ly(
  type = "funnelarea",
  values = c(5, 4, 3, 2, 1),
  text = c("The 1st","The 2nd", "The 3rd", "The 4th", "The 5th"),
  marker = list(colors = c("deepskyblue", "lightsalmon", "tan", "teal", "silver"),
                line = list(color = c("wheat", "wheat", "blue", "wheat", "wheat"), 
  width = c(0, 1, 5, 0, 4))),
  textfont = list(family = "Old Standard TT, serif", size = 13, color = "black"),
  opacity = 0.65,
  hovertemplate = paste('%{value}<extra></extra>' ,
                        '%{text}<extra></extra>'))
                          <img src="https://i.stack.imgur.com/Q8XYR.png" width="330" height="330">
...