How to add line breaks to plotly hover labels

£可爱£侵袭症+ 提交于 2019-11-29 06:08:47

问题


Is there a way to get plotly to display the hover text on multiple lines/get it to recognize special characters line '\n' in the text?

A dummy version of what I'm looking to do is:

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")

Which of course just ignores the separator and prints all on one line. Can I trick plotly/R into recognizing that line break?


回答1:


Just use the HTML tag <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")

Additionally, you could use HTML tags to change the font styles as well (and much more)...



来源:https://stackoverflow.com/questions/34878695/how-to-add-line-breaks-to-plotly-hover-labels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!