如何修改R语言Plotly雷达图悬浮弹窗的r与theta标签
解决方案
完全可以实现这个需求,核心是通过customdata参数传递完整问题和值标签,再在hovertemplate中调用这些自定义数据来替换默认显示内容。
修改后的代码如下:
plot_df <- data.frame( questions = c('level of expertise?', 'willing to work', 'motivation', 'Its friday', 'Chuck Norris'), question_short = c('LOE', 'WTW', 'mtvtn', 'frdy', 'ChuNor'), values_label = c("Very Low", "Low", "Medium", "High", "Very High"), values = c(0:4) ) fig_topic <- plot_ly( type = 'scatterpolar', fill = 'toself' ) %>% add_trace( r = plot_df$values, theta = plot_df$question_short, # 传递自定义数据:完整问题和值标签 customdata = cbind(plot_df$questions, plot_df$values_label), name = "Survey Response", # 原代码中group_id列不存在,此处替换为合理名称 hovertemplate = paste( '<b>Question</b>: %{customdata[0]}<br>', '<i>Value label</i>: %{customdata[1]}<br>', '<b>Numerical value</b>: %{r}<br>', '<extra></extra>' # 移除弹窗右上角默认的trace名称,简化显示 ) ) fig_topic
关键修改说明:
customdata参数:将questions和values_label两列绑定为二维数组,为每个数据点附加自定义信息。hovertemplate调整:- 用
%{customdata[0]}调用完整问题文本 - 用
%{customdata[1]}调用值标签 - 保留
%{r}显示原始数值(按需选择是否保留) <extra></extra>用于隐藏弹窗右上角默认的trace名称,让悬浮内容更简洁
- 用
修改后,雷达图坐标轴仍显示短名称(保持图表简洁),鼠标悬浮时会展示完整问题、值标签和对应数值,完全匹配你的需求。
内容的提问来源于stack exchange,提问作者IgorG
相关产品推荐
相关产品推荐

