简体   繁体   English

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

[英]How to colour part of ggplotly/plotly hover text a different colour in R

I would like to colour part of the hover text another colour in a ggplotly object.我想在 ggplotly 对象中为悬停文本的一部分着色另一种颜色。 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.我可以加粗Sepal Length,但不能将Sepal Width 标题更改为红色。 Help would be greatly appreciated!帮助将不胜感激! 从上面的代码中绘制

You could achieve your desired result by using a span instead of a p tag:您可以通过使用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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM