简体   繁体   English

如何使用 R 将带有希腊字母的标题添加到 Plotly 图中?

[英]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.我在 R 中有一个Plotly直方图,我想向其中添加以下标题。 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.显然Plotly不接受来自plotmath expression()所以上面的代码失败了。 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?有人可以帮我吗?

方程


I also tried this solution as Plotly often borrows from CSS ( tutorial ).我也尝试过这个解决方案,因为 Plotly 经常从CSS教程)中借用。 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.但是, add_text(x=10, y=10, text="<b>theta = 0.8*theta_hat</b>")确实生成了粗体文本,但是add_text(x=50, y=50, text="<p>&theta</p>"add_text(x=50, y=50, text="<p>&theta;</p>"不会生成希腊字母。

You can use Latex, as suggested at this link .您可以按照此链接的建议使用 Latex 。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM