简体   繁体   中英

Plotting with Knitr in Lyx

I'm trying to plot using Knitr in Lyx. 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 = 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)

<<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.

First, install packages separately, just running install.packages in pure R. Second, you do not define y .

Here's a minimal example that produces a plot without showing R code, warnings or messages:

<<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? Just re-run it to make sure it is. If everything is ok there, then it might be something with the LATEX/knitr installation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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