Forum: support
Monitor Forum | | RE: Unwanted image placement in rendered exam with exam2nops [ Reply ] By: Filipe Alberto on 2025-11-22 16:41 | [forum:50127] |
| Thanks for the tip on the axis(), much simpler. | |
| RE: Unwanted image placement in rendered exam with exam2nops [ Reply ] By: Achim Zeileis on 2025-11-22 06:36 | [forum:50126] |
|
Sorry for the confusion regarding fig.cap: If the line preceeding the ```{r} figure chunk is _not_ empty, e.g., just contains \, then knitr also does not produce a figure caption, even if you just omit the fig.cap argument. If there is a blank line, then you need fig.cap="". Regarding the figure placement: My personal feeling is, that this is all overkill. If this were my exercise I would boil it down to the code below. I've tried to boild everything down to the essentials (including the axis formatting). Question ======== If you had data that looked like this, what method might you use to test for an association between Y (left axis) and X (bottom axis)? ```{r, echo=FALSE, fig.width=5.5, fig.height=4, fig.cap=""} x <- seq(-50, 50, 10) y <- (-10 * (x^2) + (20 * x) + 300) plot(x, y, col = "red4", pch = 16, axes = FALSE, ylim = c(-22000, 500)) axis(1, at = c(-100, 100)) axis(2, at = c(-100000, 10000)) ``` Answerlist ---------- * Quadratic (polynomial) regression * Spearman correlation * Linear regression * ANOVA Meta-information ================ exname: nonlinear extype: schoice exsolution: 1000 exshuffle: TRUE |
|
| RE: Unwanted image placement in rendered exam with exam2nops [ Reply ] By: Filipe Alberto on 2025-11-21 19:24 | [forum:50123] |
|
Cool, that worked by removing fig.pos. However, I had to leave fig.cap="", otherwise I was getting a caption by default named with the name of the R the R chunck, like this Figure X. Plot of [name of chunk]. I could, like you suggested, use text to label the figure. However, I think I will go with a different solution: write the plot with png() followed by dev.off(), then use the aforementioned LaTeX code to read the plot. Like this, figure captions get labeled sequentially and without weird preceding strings. Something like below is working ```{r , echo = FALSE, results = "hide", fig.width = 4.8,fig.height=3.5,fig.cap="Association between X and Y variables"} x<-seq(-50,50,10) y<-(-10*(x^2)+(20*x)+300) png(file="nonlinearplot.png") par(cex=1.8) plot(x,y,col="red4",pch=16,axes=F,ylim=c(-22000,500)) axis(1,at=x,labels=rep("",length(x)),lwd.ticks=0) axis(2,pos=-50,at=y,labels=rep("",length(y)),lwd.ticks=0) dev.off() ``` \begin{figure}[H] \centering \includegraphics[width=16cm, height=9cm]{nonlinearplot.png} \caption{Association between X and Y variables} \end{figure} |
|
| RE: Unwanted image placement in rendered exam with exam2nops [ Reply ] By: Achim Zeileis on 2025-11-21 18:23 | [forum:50122] |
|
You use both fig.pos and fig.cap which consequently creates a floating {figure} environment. With the H position you can enforce that the figure is placed "Here". But IMO a simpler solution is to omit fig.pos and fig.cap altogether. Then only the \includegraphics{} is created _without_ a floating {figure} environment. Then you also include the figure in place. I would then just include the annotation/caption of the figure in the regular text. For example, In your example exercise you have already done this anyway so that the caption can easily be dropped. |
|
| RE: Unwanted image placement in rendered exam with exam2nops [ Reply ] By: Filipe Alberto on 2025-11-21 16:07 | [forum:50121] |
|
I tried boxplots and it works fine. My own question (nonlinear.Rmd, see below) is still causing me problems. I looked at the part of the boxplots.Rmd that produced the plot and can't seem to find what might be causing that example to place the figure always below the question, while mine doesn't. For images imported from outside, I seem to have found a solution with usepackage="float" and HTML code with [H] controller. To insert the figure I used this. \begin{figure}[H] \includegraphics[width=16cm, height=9cm]{/home/filipe/Documents/jobs/UW-Milwaukee/Teaching/Rexams/images/Correlation_sign.png} \caption{Draw points here to represent the associations.} \end{figure} Below is the top part of the code for my question nonlinear.Rmd Question ======== If you had data that looked like this, what method might you use to test for an association between Y (left axis) and X (bottom axis) ? ```{r , echo = FALSE, fig.pos='H', fig.width = 4.8,fig.height=3.5,fig.cap="Association between X and Y variables"} x<-seq(-50,50,10) y<-(-10*(x^2)+(20*x)+300) plot(x,y,col="red4",pch=16,axes=F,ylim=c(-22000,500)) axis(1,at=x,labels=rep("",length(x)),lwd.ticks=0) axis(2,pos=-50,at=y,labels=rep("",length(y)),lwd.ticks=0) ``` Answerlist ---------- * Quadratic (polynomial) regression * Spearman correlation * Linear regression * ANOVA Solution ======== ... |
|
| RE: Unwanted image placement in rendered exam with exam2nops [ Reply ] By: Achim Zeileis on 2025-11-21 15:32 | [forum:50120] |
| Can you provide a minimal reproducible example? Ideally just using one of the exercises shipped along with the package (like Rlogo.Rmd, boxplots.Rmd, etc.). | |
| Unwanted image placement in rendered exam with exam2nops [ Reply ] By: Filipe Alberto on 2025-11-21 15:26 | [forum:50119] |
|
I am having a hard time placing figures from the individual questions in the correct place in the rendered pdf containing the exam. I have used markdown syntax like below  Or from an R chunk like below ```{r , echo = FALSE, fig.width = 4.8,fig.height=3.5,fig.cap="Association between X and Y variables"} x<-seq(-50,50,10) y<-(-10*(x^2)+(20*x)+300) plot(x,y,col="red4",pch=16,axes=F,ylim=c(-22000,500)) axis(1,at=x,labels=rep("",length(x)),lwd.ticks=0) axis(2,pos=-50,at=y,labels=rep("",length(y)),lwd.ticks=0) ``` In either case, the figures will appear in unwanted places, inserted into the body of the wrong question. Any thoughts on how to solve this issue? |
|

