r - 在绘图仪表图中添加悬停文本或文本注释
问题描述
我们可以在plotly
仪表图中添加悬停文本或文本注释吗?例如,在我下面的情节中,我想在绿色区域中添加悬停或文本或两者"Uptake first dose%: 19.8"
以及灰色区域"Not vaccinated (%):80.2"
library(plotly)
fig <- plot_ly(
type = "indicator",
mode = "gauge+number+delta",
value = 19.8,
title = list(text = "Uptake first dose %", font = list(size = 24)),
delta = list(reference = 70, increasing = list(color = "gray")),
gauge = list(
axis = list(range = list(NULL, 100), tickwidth = 1, tickcolor = "lightgreen"),
bar = list(color = "lightgreen"),
bgcolor = "white",
borderwidth = 2,
bordercolor = "gray",
steps = list(
list(range = c(0, 50), color = "lightgreen"),
list(range = c(20, 100), color = "gray")),
threshold = list(
line = list(color = "black", width = 4),
thickness = 0.75,
value = 70)))
fig <- fig %>%
layout(
margin = list(l=20,r=30),
paper_bgcolor = "lavender",
font = list(color = "darkblue", family = "Arial"))
fig
解决方案
您可以添加注释,但我找不到在没有注释的情况下获取悬停文本的方法。
fig <- plotly::plot_ly(
type = "indicator",
mode = "gauge+number+delta",
value = 19.8,
title = list(text = "Uptake first dose %", font = list(size = 24)),
delta = list(reference = 70, increasing = list(color = "gray")),
gauge = list(
axis = list(range = list(NULL, 100), tickwidth = 1, tickcolor = "lightgreen"),
bar = list(color = "lightgreen"),
bgcolor = "white",
borderwidth = 2,
bordercolor = "gray",
steps = list(
list(range = c(0, 50), color = "lightgreen"),
list(range = c(20, 100), color = "gray")),
threshold = list(
line = list(color = "black", width = 4),
thickness = 0.75,
value = 70)))
fig <- fig %>%
plotly::layout(
margin = list(l=20,r=30),
paper_bgcolor = "lavender",
font = list(color = "darkblue", family = "Arial"),
annotations = list(x = 0.05, y = 0.3, text = "19.8%",
hovertext = "Your Text",
showarrow = FALSE))
fig
add_annotations
如果要添加倍数,也可以使用。见https://plotly.com/r/text-and-annotations/
推荐阅读
- c# - 如何获取 rasdial API 的 PPP cookie?
- php - Stripe:如何在 php 中从 Charge API 迁移到 PaymentIntent API
- excel - 在不清除过滤器的情况下设置 Excel 范围的值
- python - 在远程桌面上使用 PyAutoGUI 的鼠标单击问题
- java - 空手道 0.9.5.RC5 -“重试直到”功能不知何故不尊重我在 karate-config.js 中的“重试”配置
- r - 清理具有重复但不同因子级别的起点和终点数据
- ruby - 如何使用代理运行 ruby 脚本?
- r - 将一个集合拆分为 n 个不相等的子集,关键决定因素是子集中的元素聚合并等于预定数量?
- excel - 将特定单词表的内容复制到excel
- python - 如何用 Pandas 总结某些行并将结果添加到 defaultdict(大型数据集)