简体   繁体   English

在Lyx中用Knitr绘图

[英]Plotting with Knitr in Lyx

I'm trying to plot using Knitr in Lyx. 我正在尝试在Lyx中使用Knitr进行绘图。 When I run 当我跑步

<<>>=
install.packages("ggplot2")
library(ggplot2)
qplot(y=y, x=1:1000, main = 'Log-Likelihood')
@

I get the error 我得到错误

LaTeX Error: File `figure/unnamed-chunk-6.eps.bb' not found.

I've tried including extensions in the starting brackets, but with no success. 我尝试在起始括号中包含扩展名,但没有成功。 How do I get my plot? 我如何得到我的情节?


Following the first answer, tried this: 按照第一个答案,尝试以下操作:

Defining function (not that important, just to show how I get y) 定义功能(不是很重要,只是为了说明如何获得)

<<>>=
exp.loglik <- function(lambda, obs){   
    xbar = mean(obs)   
    return(length(obs)*log(lambda)-lambda*xbar) 
}
@

Defining y (not that important, but just including to show how y is defined) 定义y(不那么重要,仅包括显示y的定义)

<<>>=
y = rep(NA,1000) 
for (i in 1:1000){   
    y[i] = exp.loglik(lambda=i/10000, obs=diet_data$survtime)   
}
@

Code that runs and then the error occurs (note that I installed the package in pure R as instructed) 运行代码,然后发生错误(请注意,已按照说明将软件包安装在纯R中)

<<warning=FALSE, message=FALSE, echo=FALSE>>=
library(ggplot2)
qplot(y=y, x=1:1000, main = 'Log-Likelihood')
@

Same ERROR: LaTeX Error: File `figure/unnamed-chunk-6.eps.bb' not found. 相同的错误:LaTeX错误:找不到文件`figure / unnamed-chunk-6.eps.bb'。

First, install packages separately, just running install.packages in pure R. Second, you do not define y . 首先,单独安装软件包,仅在纯R中运行install.packages 。第二,您不定义y

Here's a minimal example that produces a plot without showing R code, warnings or messages: 这是一个不显示R代码,警告或消息而生成绘图的最小示例:

<<warning=FALSE, message=FALSE, echo=FALSE>>= 
library(ggplot2) 
qplot(y=10:1, x=1:10, main = 'Log-Likelihood') 
@

Edit : 编辑

I am running the following code: 我正在运行以下代码:

<<>>= 
exp.loglik <- function(lambda, obs) {        
  xbar = mean(obs)        
  return(length(obs)*log(lambda)-lambda*xbar)  
}
@

<<>>= 
y = rep(NA,5)  
for (i in 1:5) {        
  y[i] = exp.loglik(lambda=i/5, obs=runif(5))    
} 
@

<<warning=FALSE, message=FALSE>>= 
library(ggplot2) 
qplot(y=y, x=1:5, main = 'Log-Likelihood') 
@

and I get a picture. 然后我得到一张照片。 Is your code working in clean R? 您的代码在干净的R中工作吗? Just re-run it to make sure it is. 只需重新运行它以确保它是。 If everything is ok there, then it might be something with the LATEX/knitr installation. 如果一切正常,则可能是LATEX / knitr安装。

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

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