如何为ggplotly/plotly悬停文本的一部分颜色在r中呈现不同的颜色

发布于 2025-02-03 09:59:05 字数 739 浏览 3 评论 0原文

我想在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!
Plot as is from code above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2025-02-10 09:59:05

您可以使用span而不是p标签来实现所需的结果:

library(plotly)
library(ggplot2)
library(dplyr)

iris_in <- iris %>%
  mutate(mytext = paste0("<b>Sepal length:</b> ", Sepal.Length, "\n",
                         "<span style='color:red'>Sepal width: </span>", 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

“

You could achieve your desired result by using a span instead of a p tag:

library(plotly)
library(ggplot2)
library(dplyr)

iris_in <- iris %>%
  mutate(mytext = paste0("<b>Sepal length:</b> ", Sepal.Length, "\n",
                         "<span style='color:red'>Sepal width: </span>", 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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文