我无法将R sweave中创建的图表转换为Pdf。但是可以创建其他图表。是否需要为情节图添加任何其他代码?
以下是一个例子
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo = FALSE>>=
C1<-c("A","B","C")
C2<-c(10,20,30)
Df<-data.frame(C1,C2)
@
\begin {figure}
<<echo = FALSE, fig = TRUE>>=
suppressWarnings (library (plotly))
plot_ly(Df, x = ~C1, y = ~C2, type = "bar")%>% layout(title = "ABC")
@
\caption {PlotlyChart}
\end {figure}
Normal Chart
\begin {figure}
<<echo = FALSE, fig = TRUE>>=
barplot(Df[ ,2], names.arg = Df[ ,1])
@
\caption {Normal_Chart}
\end {figure}
\end{document}
当我在R中尝试普通的盒子图时,它可以工作。只有当我添加Plotly图表时,才会收到错误消息
Running pdflatex on ABC.tex...failed
Error running /Library/TeX/texbin/pdflatex (exit code 1)
我是R sweave和Latex的新手,所以任何帮助都表示赞赏。谢谢!
答案 0 :(得分:2)
Plotly生成绘制情节的javascript代码。此代码将在Web浏览器中执行。仅在生成HTML文档时可以使用Plotly。没有PDF,没有Latex。
但您可以将包webshot
与phantomjs
一起使用,将图表保存为图像,然后将图像包含为图形。
首先安装webshot:
install.packages("webshot")
webshot::install_phantomjs()
然后修改你像这样的Sweave文件:
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo = FALSE>>=
C1<-c("A","B","C")
C2<-c(10,20,30)
Df<-data.frame(C1,C2)
@
<<echo = FALSE, fig = FALSE>>=
suppressWarnings (library (webshot))
suppressWarnings (library (plotly))
py <- plot_ly(Df, x = ~C1, y = ~C2, type = "bar")%>% layout(title = "ABC")
export(py, file = "myplot.png")
@
\begin {figure}
\includegraphics{myplot.png}
\end {figure}
\end{document}