Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make plotly annotation font bold

Tags:

r

fonts

plotly

I'd like to add annotation to a scatter plot generated by R's plotly package, and have the text appear in bold.

I'm trying:

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

Which gives: enter image description here

Trying to add face="bold" to the font specification 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"))

Doesn't really change anything: enter image description here

So the question is how to get that text annotation to appear in bold.

P.S. In my real data I'd like to annotate clusters of points hence the annotations come as a separate layer.

like image 301
dan Avatar asked Apr 27 '18 17:04

dan


People also ask

How do you make a title bold in Plotly R?

One method for getting bold text is to change the font to Arial Black (or other bold font) which should be available on most systems. This method will scale a little easier to axes and other elements. Save this answer.

What font does Plotly use?

The default font family spec is "Open Sans", verdana, arial, sans-serif , as listed in Python Figure Reference: layout from the Plotly documentation. The precise font selected depends on which fonts you have installed on your system. If Open Sans is available, that font will be selected.


1 Answers

Simply enter your text as HTML, like so:

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))
like image 91
Tobias Krabel Avatar answered Oct 03 '22 19:10

Tobias Krabel