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

时间:2016-01-19 13:56:30

标签: r plotly

有没有办法让你在多行上显示悬停文字/让它识别文字中的特殊字符'\ 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")

当然,这会忽略分隔符并在一行上打印所有内容。我可以通过剧情/ R来识别换行吗?

1 个答案:

答案 0 :(得分:38)

只需使用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标签更改字体样式(以及更多)......