How to add a title with greek letters to a Plotly graph with R?

2.8k views Asked by At
library(plotly)

f <- plot_ly(alpha = 0.5) %>% 
            add_histogram(x=rnorm(1000), 
                          name="Observed",
                          marker=list(color="darkolivegreen")
                         )
            )

f %>% layout(title=expression(theta==0.8%*%hat(theta)))

I have a Plotly histogram in R, and I want to add the following title to it. It doesn't have to be a title; just a text saying that in the blank area of the graph will do. Apparently Plotly doesn't accept expression() from plotmath so the code above fails. It's the hat part that makes me really wanna show it as a proper equation; would be so much clearer. Can anybody help me out here?

equation


I also tried this solution as Plotly often borrows from CSS (tutorial). However, add_text(x=10, y=10, text="<b>theta = 0.8*theta_hat</b>") did generate a bold text, but add_text(x=50, y=50, text="<p>&theta</p>" or add_text(x=50, y=50, text="<p>&theta;</p>" does not generate greek letters.

1

There are 1 answers

2
Marco Sandri On BEST ANSWER

You can use Latex, as suggested at this link.

f <- plot_ly(alpha = 0.5) %>% 
            add_histogram(x=rnorm(1000), 
                          name="Observed",
                          marker=list(color="darkolivegreen")
                         )
   
f %>% layout(title=TeX("\\theta = 0.8 \\hat\\theta")) %>% 
      config(mathjax = 'cdn')

enter image description here