Sys.setenv(PLOTLY_MATHJAX_PATH="/opt/mathjax")
# PLOTLY_MATHJAX_PATH 环境变量指向 MATHJAX
x = seq(from = -4, to = 8, length.out = 193)
y1 = dnorm(x, mean = 3, sd = 1)
y2 = dnorm(x, mean = 2, sd = 1.5)
library(magrittr)
hline <- function(y = 0, color = "red") {
list(
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = y,
y1 = y,
line = list(color = color, dash = 'dash')
)
}
vline <- function(x = 0, color = "red") {
list(
type = "line",
x0 = x,
x1 = x,
yref = "paper",
y0 = 0,
y1 = 1,
line = list(color = color, dash = 'dash')
)
}
p = plotly::plot_ly(
x = x, y = y1,
type = "scatter", mode = "lines",
fill = "tozeroy", fillcolor = "rgba(92, 184, 92, 0.2)",
text = ~ paste0(
"x:", x, "<br>",
"y:", round(y1, 3), "<br>"
),
hoverinfo = "text",
name = plotly::TeX("\\mathcal{N}(3,1^2)"),
line = list(shape = "spline", color = "#5CB85C")
) %>%
plotly::add_trace(
x = x, y = y2,
type = "scatter", mode = "lines",
fill = "tozeroy", fillcolor = "rgba(91, 192, 222, 0.2)",
text = ~ paste0(
"x:", x, "<br>",
"y:", round(y2, 3), "<br>"
),
hoverinfo = "text",
name = plotly::TeX("\\mathcal{N}(2, 1.5^2)"),
line = list(shape = "spline", color = "#5BC0DE")
) %>%
plotly::layout(
xaxis = list(showgrid = F, title = plotly::TeX("x")),
yaxis = list(showgrid = F, title = plotly::TeX("f(x)")),
legend = list(x = 0.8, y = 1, orientation = "v")
) %>%
plotly::config(displayModeBar = FALSE, mathjax = "local")
# 导出静态图形
plotly::orca(p, file = "plot.png", mathjax = T)
报如下错误
Error in mathjax_local() :
Couldn't locate necessary MathJax config: TeX-AMS-MML_SVG
把 mathjax_local 函数翻出来看了下
plotly:::mathjax_local
function ()
{
path <- mathjax_path()
mj <- file.path(path, "MathJax.js")
if (!file.exists(mj))
stop("Couldn't locate MathJax.js")
mathjax <- readLines(mj)
pat <- "MathJax.fileversion=\"[0-9].[0-9].[0-9]"
ver <- regmatches(mathjax, regexpr(pat, mathjax))
ver <- sub("\"", "", strsplit(ver, "=")[[1]][2])
config <- file.path(path, "config", "TeX-AMS-MML_SVG.js")
if (!file.exists(config))
stop("Couldn't locate necessary MathJax config: TeX-AMS-MML_SVG")
htmltools::htmlDependency(name = "mathjax", version = ver,
src = path, script = c("MathJax.js", "config/TeX-AMS-MML_SVG.js"))
}
<bytecode: 0x7fd92d6ecb98>
<environment: namespace:plotly>
MathJax config 是什么东西呢?MathJax ( https://github.com/mathjax/MathJax ) 找不到 TeX-AMS-MML_SVG.js
参考
https://plotly-r.com/mathjax.html