-1

I am using sweave to drawing plots and I want to fix the position of R code without fix the position of graphs, that's to say I would like to keep the position of code in the text but have graphs floating, what should I do? Thank you

It sesms that it's not clear so I put the code there:

text before the figure

\begin{figure}
<<smokefig, fig.width=3, fig.height=3>>=
tabSmoke<-table(ps$smoke)[2:5]
par(mar = c(2, 4.1, 0.1, 0.1))
with(ps,barplot(100*tabSmoke/sum(tabSmoke),ylim=c(0, 110), las = 2, 
                cex.axis = 0.75,cex.names = 0.75,col = c("pink","lightblue")))
@
\caption{Pourcentages des fumeurs et non-fumeurs selon les quatre modalités}
\end{figure}

text after the figure

what I want is to have R code appears between the line, like

text before the figure

tabSmoke<-table(ps$smoke)[2:5]
par(mar = c(2, 4.1, 0.1, 0.1))
with(ps,barplot(100*tabSmoke/sum(tabSmoke),ylim=c(0, 110), las = 2, 
                cex.axis = 0.75,cex.names = 0.75,col = c("pink","lightblue")))

text afther the figure

and then the figure could appear wherever it wants
but it turns out that the code floats with the figure and they appear together at the next page like this:
output of code lines
output of figure

This is what I have put in header, in case it might be helpful

\documentclass[10pt]{article}
\usepackage{geometry}
\geometry{a4paper,scale=0.75}
\author{me}
\title{title}
\begin{document}
ruiruiyang
  • 13
  • 4

1 Answers1

0

You say you're using Sweave. It's not clear from what you posted whether you mean the Sweave() function in R, or the Sweave format in knitr. It's a lot easier in the latter; you should switch to it if you aren't already using it.

Here's how to write your figure so it displays the way you want. The main change is to use fig.cap to specify the caption in the chunk header; then knitr will automatically write out the LaTeX environment for the figure, add the caption, etc.

text before the figure

<<smokefig, fig.width=3, fig.height=3, fig.cap = "Pourcentages des fumeurs et non-fumeurs selon les quatre modalités">>=
tabSmoke<-table(ps$smoke)[2:5]
par(mar = c(2, 4.1, 0.1, 0.1))
with(ps,barplot(100*tabSmoke/sum(tabSmoke),ylim=c(0, 110), las = 2, 
                cex.axis = 0.75,cex.names = 0.75,col = c("pink","lightblue")))
@

text after the figure

If you want to do this in Sweave(), you put the code outside of the figure environment, producing a file and not including it; then use \includegraphics in the figure environment to include the figure. Definitely more work and more error-prone, so don't do it. Switch to knitr.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • I'm sorry for the ambiguity cause I'm new to R. Thank you for your responce! – ruiruiyang Apr 09 '20 at 16:46
  • Did my answer solve your problem? If not, you could edit your question to explain what's wrong, or leave another comment here. If it did solve the problem, you should mark it as answered by clicking on the checkmark beside it. – user2554330 Apr 09 '20 at 18:28