在R的Highchart中如何修改散点图的悬停标签
解决Highchart散点图悬停标签修改问题
你可以通过添加hc_tooltip()配置来自定义悬停提示框内容,实现移除x项、将"y"改为"A"的需求,以下是修改后的完整代码:
df1 <- structure(list(col1 = c("A", "B", "C", "D", "E", "F"), col2 = c(3.54172960518873, 4.51316494632718, 8.83778709982866, 8.9301479840624, 12.5459778934778, 12.7053222878864)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) df1 %>% hchart( "scatter", hcaes(x = col1, y = round(col2,2)), showInLegend = TRUE, color = "#63696b", name = "Title 1" ) %>% hc_title(text ="RMSE", align = "center", style = list(fontSize = "18px", fontFamily = "Roboto Condensed", fontWeight = "bold")) %>% hc_yAxis( title = list(text = glue::glue("Title 2"), name = "RMSE", style = list(fontSize = "12px", color = "black", fontWeight = "900", fontFamily = "Roboto Condensed"), labels = list(enabled = TRUE) )) %>% # 自定义悬停提示框 hc_tooltip( pointFormat = "<b>A:</b> {point.y}" )
关键说明:
hc_tooltip()是Highcharter中配置悬停提示框的核心函数pointFormat参数直接定义提示框显示内容:<b>A:</b>把默认的"y"替换为你需要的"A",<b>是加粗样式(不需要可直接去掉){point.y}调用散点对应的y轴数值,也就是你提前处理好的round(col2,2)结果
- 这个配置会自动移除默认显示的x项,只保留你定义的内容
如果需要更复杂的自定义逻辑,也可以用formatter参数结合JS函数实现:
hc_tooltip( formatter = JS("function() { return '<b>A:</b> ' + this.point.y; }"), useHTML = TRUE )
内容的提问来源于stack exchange,提问作者Laura
相关产品推荐
相关产品推荐

