2
votes

I am aiming at including a knitr plot in LyX that I can cross-reference in my document. I:

  1. Inserted a float image.
  2. Added a caption and a label.
  3. Inserted a ERT instead of an image in the float image.

An image of what I did is here:

Cross-refernce knitr figure in LyX

I checked here at SO similar questions and nobody did what I've done so here I ask:

  • Is there a simpler way to do what I am doing?
  • How can I center the knitr image?

The code for my image is:

<<echo=FALSE>>=
dat <- data.frame(x=rnorm(100), y1=rnorm(100)-1, y2=rnorm(100), y3=rnorm(100)+1) 
windows() 
plot(y1 ~ x, data=dat, type='n', axes=FALSE, xlab="", ylab="")      
text(dat$x,dat$y1,label=round(runif(100,0,100),0),col='blue', cex = abs(rnorm(100,0,1)))
text(dat$x,dat$y2,label=round(runif(100,0,100),0),col='purple', cex = abs(rnorm(100,0,1)))
text(dat$x,dat$y3,label=round(runif(100,0,100),0),col='red', cex = abs(rnorm(100,0,1)))
@

Thanks for looking into this. Your time is appreciated.

1

1 Answers

1
votes
  • How can I center the knitr image?

Alright, I figured out how to center the knitr image in a float image in LyX:

Create a Float Image and caption it...

Click on top of the caption and then right click to get Paragraph Settings so that you can center the ERT before inserting the ERT.

Center the ERT

Insert knitr chunk: Insert chunk

You are done: the image is centered and it can be cross-referenced in your LyX file...

For the other question:

  • Is there a simpler way to do what I am doing?

Perhaps there is but in LyX, by clicking on Help - Specific Manuals - Sweave, there is something similar to what I am doing as shown here:

Sweave's Manual in LyX screenshot

So I guess we're done with this...