r - 如何在漏斗图中固定大小但动态悬停文本?
问题描述
我正在尝试使用 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
结果
解决方案
我想显示上面的漏斗图;但在悬停时我想显示不同的文字。隧道仅用于表示形状,但悬停将显示实际值。
根据 OP 的上述评论,我猜他们想要 hoverinfo 从这个向量中选择它的文本 values 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>')
可以添加更多文本/值。下面是一个例子。你可以在这里阅读更多内容: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>'))
推荐阅读
- c++ - 映射二维数组以进行寻路的最佳方法?
- python - 使用 % 运算符在 python 中打印浮点数
- php - 在 Wordpress 网站上引用 MYSQL 数据库中的数据
- amazon-web-services - 从 Github 获取 AWS CodeDeploy 到 Auto Scale Group 的最新提交
- javascript - 过渡属性的跳跃行为
- java - 条形图左右移动
- linux - 构建内核模块时如何链接静态库?
- laravel - 添加修订后自动更改状态
- python-3.x - 我想从文本输入中提取第三行
- java - 尝试构建 gradle 时出错 - androidx.appcompat:appcompat:28.+