r - 如何在漏斗图中具有固定大小但动态悬停文本?

标签 r hover plotly r-plotly

我正在尝试使用 R 制作漏斗图。

问题是数字存在偏差,并且没有统一的表示形式。然而,我们想展示流程。那么如何在 R 中制作漏斗图,其中漏斗的大小是固定的,但文本是动态的并且来自数据框列。

查看源代码示例: 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

最佳答案

我想显示如上所示的漏斗图;但悬停时我想显示不同的文本。隧道仅用于表示形状,但悬停将显示实际值。

根据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>')
                          

It's possible to add more text/values. Below is an example of that. You can read more here: 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>'))
                          

关于r - 如何在漏斗图中具有固定大小但动态悬停文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720080/

相关文章:

c++ - Rcpp 中的逐元素矩阵乘法

r - 基于 R 中的序列高效计算总和

jquery - 为什么升级 jQuery 会破坏 jQuery UI 功能?

html - 悬停效果不会在图像上触发

python - Plotly:如何更改 3D 曲面图的配色方案?

r - 将 r-markdown 编织成 PDF 时,有没有办法将 LaTeX 引用键保留在 .tex 文件中

r - 为出现多次的观测值分配一个主 ID

jquery - 在 jQuery 中悬停表格行时存储背景颜色

python - 共享相同颜色条的绘图堆叠表面

r - 如何将 R 中的自定义悬停文本添加到两个相互引用的系列中