Paw in Data
Paw in Data

Reputation: 1564

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

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.

Upvotes: 0

Views: 3198

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24262

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

Upvotes: 1

Related Questions