使Plotly注释字体加粗

10

我想在使用Rplotly包生成的scatter图上添加annotation,并使文本加粗。

我尝试过:

library(plotly)
library(dplyr)

set.seed(1)
df <- data.frame(x=rnorm(10),y=rnorm(10))
plotly::plot_ly(x =~ df$x, y =~ df$y,marker = list(size=12), type = 'scatter',mode = "markers") %>%
  plotly::add_annotations(text=1:10,showarrow=T,arrowhead=1,x=df$x,y=df$y,font=list(size=10))

这给出的是:

enter image description here

试图将 face="bold" 添加到 font 规范的 list 中:

plotly::plot_ly(x =~ df$x, y =~ df$y,marker = list(size=12), type = 'scatter',mode = "markers") %>%
  plotly::add_annotations(text=1:10,showarrow=T,arrowhead=1,x=df$x,y=df$y,font=list(size=10,face="bold"))

实际上并没有改变什么:

图片描述在这里输入

因此问题是如何使文本注释以粗体方式显示。

P.S. 在我的真实数据中,我想要注释点的聚类,因此注释作为独立的图层出现。

1个回答

14

只需将您的文本以 HTML 格式输入,例如:

plotly::plot_ly(x =~ df$x, y =~ df$y,marker = list(size=12), type = 'scatter',mode = "markers") %>%
  plotly::add_annotations(text=sprintf("<b>%s</b>", 1:10),showarrow=T,arrowhead=1,x=df$x,y=df$y,font=list(size=10))

1
这在python中也适用。将您的文本放在<b></b>之间。 - LucG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接