首页 > 解决方案 > 使情节注释字体加粗

问题描述

我想添加annotation到由's包scatter生成的绘图中,并让文本以粗体显示。Rplotly

我正在努力:

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

这使: 在此处输入图像描述

试图添加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"))

并没有真正改变任何东西: 在此处输入图像描述

所以问题是如何让文本注释以粗体显示。

PS 在我的真实数据中,我想注释点簇,因此注释作为单独的层出现。

标签: rfontsplotly

解决方案


只需将文本输入为 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))

推荐阅读