如何为ggplotly/plotly悬停文本的一部分颜色在r中呈现不同的颜色
我想在ggplotly对象中为悬停文本的部分颜色染色。大胆和斜体很容易,但是字体颜色似乎并不容易。我认为下面的代码应该
使用虹膜数据集来使用示例:
library(plotly)
library(ggplot2)
library(htmltools)
library(dplyr)
iris_in <- iris %>%
mutate(mytext = paste0("<b>Sepal length:</b> ", Sepal.Length, "\n",
p(text = "Sepal width: ", style="color:red"), Sepal.Width))
p <- ggplot() +
geom_point(data = iris_in, aes(x = Sepal.Length, y = Sepal.Width, color = Species, text = mytext))
p <- ggplotly(p, tooltip = "mytext")
p
I would like to colour part of the hover text another colour in a ggplotly object. It is easy to bold and italicise but it doesn't seem to be the same ease for font colour. I thought the code below should work
Example with the Iris data set:
library(plotly)
library(ggplot2)
library(htmltools)
library(dplyr)
iris_in <- iris %>%
mutate(mytext = paste0("<b>Sepal length:</b> ", Sepal.Length, "\n",
p(text = "Sepal width: ", style="color:red"), Sepal.Width))
p <- ggplot() +
geom_point(data = iris_in, aes(x = Sepal.Length, y = Sepal.Width, color = Species, text = mytext))
p <- ggplotly(p, tooltip = "mytext")
p
I can bold Sepal Length but I can't change the Sepal Width title to red. Help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
span
而不是p
标签来实现所需的结果:You could achieve your desired result by using a
span
instead of ap
tag: