【问题标题】:how to fix the position of code in Sweave?如何修复 Sweave 中代码的位置?
【发布时间】:2020-04-05 19:44:55
【问题描述】:

我正在使用sweave绘制绘图,我想固定R代码的位置而不固定图形的位置,也就是说我想保持代码在文本中的位置但图形浮动,我应该怎么做做?谢谢

感觉不清楚所以我把代码放在那里:

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

我想要的是让 R 代码出现在行之间,比如

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

然后这个数字可以出现在任何它想要的地方
但事实证明,代码随数字浮动,它们一起出现在下一页,如下所示:
output of code lines
output of figure

这是我放在标题中的内容,以防它可能有用

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

【问题讨论】:

  • 我认为这是正常行为。请发布一个不起作用的自包含示例,我们会让您知道出了什么问题。
  • @user2554330 感谢您的评论,我已经编辑了我的问题

标签: r sweave


【解决方案1】:

您说您正在使用 Sweave。从您发布的内容中不清楚您是指 R 中的 Sweave() 函数,还是 knitr 中的 Sweave 格式。后者要容易得多。如果你还没有使用它,你应该切换到它。

以下是如何编写您的图形,以便它以您想要的方式显示。 主要变化是在chunk header中使用fig.cap来指定caption;然后knitr 会自动写出LaTeX 图的环境,添加标题等。

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

如果您想在Sweave() 中执行此操作,请将代码放在图形环境之外,生成一个文件而不包含它;然后在图形环境中使用\includegraphics 来包含图形。肯定更多的工作和更容易出错,所以不要这样做。切换到knitr

【讨论】:

  • 我很抱歉含糊不清,因为我是 R 新手。感谢您的回复!
  • 我的回答解决了你的问题吗?如果没有,您可以编辑您的问题以解释问题所在,或在此处留下另一条评论。如果确实解决了问题,则应通过单击旁边的复选标记将其标记为已回答。
猜你喜欢
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
相关资源
最近更新 更多