如何为 plotly 悬停标签添加换行符

How to add line breaks to plotly hover labels

有没有办法在多个 lines/get 上显示悬停文本以识别文本中的特殊字符行 '\n'?

我想要做的虚拟版本是:

data <- data.frame(cbind(rnorm(10, 8), rnorm(10, 2)))
names(data)<-c("thing1", "thing2")

data$hovertext <- paste("here's a coordinate: ",
                         round(data$thing1,1), sep = "\n")


p <- plot_ly(data, type = 'scatter', x = thing1, y = thing2, 
             text = hovertext, hoverinfo = 'text', mode = "markers")

这当然只是忽略分隔符并在一行上打印所有内容。我可以欺骗 plotly/R 识别换行符吗?

只需使用 HTML 标签 <br>:

library(plotly)
data <- data.frame(cbind(rnorm(10, 8), rnorm(10, 2)))
names(data) <- c("thing1", "thing2")

data$hovertext <- paste("here's a coordinate:",
                     round(data$thing1,1), sep = "<br>")


p <- plot_ly(data, type = 'scatter', x = ~thing1, y = ~thing2, 
         text = ~hovertext, hoverinfo = 'text', mode = "markers")

此外,您还可以使用 HTML 标签来更改字体样式(以及更多)...