R plotly禁用默认悬停文本并仅显示自定义文本
Posted
技术标签:
【中文标题】R plotly禁用默认悬停文本并仅显示自定义文本【英文标题】:R plotly disable default hovertext and only display customized text 【发布时间】:2021-10-03 11:32:43 【问题描述】:我正在努力禁用默认的悬停文本。我已经添加了自定义悬停文本,但除了自定义文本之外,默认值仍然显示。我试过 hoverinfo = 'none' 和 hoverinfo = ''。在上面的屏幕截图中,箭头指向默认的悬停文本。有没有办法禁用该文本?
这是我的代码:
temp <- structure(list(therapy_class = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("ALK Inhibitors",
"Anti-VEGF-based therapies", "EGFR TKIs", "Non-Platinum-based",
"PD-1/PD-L1-based therapies", "Platinum-based", "Single agent chemotherapies",
"Other"), class = c("ordered", "factor")), Year_month = c("Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020"),
value = c(0.028735632183908, 0.0327510917030568, 0.0239130434782609,
0.0245901639344262, 0.0237580993520518, 0.027972027972028,
0.0804597701149425, 0.0698689956331878, 0.0478260869565217,
0.0491803278688525, 0.0626349892008639, 0.0559440559440559,
0.0977011494252874, 0.111353711790393, 0.1, 0.108606557377049,
0.08207343412527, 0.0839160839160839, 0.00766283524904215,
0.0109170305676856, 0.00434782608695652, 0.00614754098360656,
0.00647948164146868, 0.0116550116550117, 0.478927203065134,
0.475982532751092, 0.456521739130435, 0.456967213114754,
0.511879049676026, 0.484848484848485, 0.162835249042146,
0.170305676855895, 0.239130434782609, 0.227459016393443,
0.183585313174946, 0.179487179487179, 0.0804597701149425,
0.0698689956331878, 0.0630434782608696, 0.0676229508196721,
0.08207343412527, 0.0955710955710956, 0.0632183908045977,
0.0589519650655022, 0.0652173913043478, 0.0594262295081967,
0.0475161987041037, 0.0606060606060606), count = c(15L, 15L,
11L, 12L, 11L, 12L, 42L, 32L, 22L, 24L, 29L, 24L, 51L, 51L,
46L, 53L, 38L, 36L, 4L, 5L, 2L, 3L, 3L, 5L, 250L, 218L, 210L,
223L, 237L, 208L, 85L, 78L, 110L, 111L, 85L, 77L, 42L, 32L,
29L, 33L, 38L, 41L, 33L, 27L, 30L, 29L, 22L, 26L)), row.names = c(NA,
-48L), class = c("tbl_df", "tbl", "data.frame"))
plot_ly(
data = temp,
y = ~value,
x = ~Year_month,
color = ~therapy_class
) %>%
add_lines(
hovertemplate = paste(temp$therapy_class,
"<br>Count:", temp$count,
"<br>PCT:", sprintf("%1.2f%%", 100*temp$value))) %>%
layout(
yaxis = list(
tickformat = "%",
title = ""
),
xaxis = list(title = ""),
legend = list(
orientation = "h", yanchor = "bottom", y = -0.5,
font = list(size = 10)
)
)
【问题讨论】:
@YBS 我要显示的信息在绿框内,即 3 行。在框外,它再次显示了治疗课程。屏幕截图中的箭头指向我不想显示的行。 【参考方案1】:plot_ly(
data = temp,
y = ~value,
x = ~Year_month,
color = ~therapy_class,
name = ""
)
需要将名称参数传递为“”。它使用默认值并在悬停信息中使用它。
【讨论】:
以上是关于R plotly禁用默认悬停文本并仅显示自定义文本的主要内容,如果未能解决你的问题,请参考以下文章