微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Sweave或Knitr中指定绘图大小以及绘图代码

如何解决如何在Sweave或Knitr中指定绘图大小以及绘图代码

我正在尝试使用Sweave或Knitr生成包含图的文档。

以下是Sweave的最小示例:

\documentclass{article}
\title{Plot example}
\begin{document}
\maketitle
<<setup>>=
library("graphics")
myplot = function(n) {
    plot(cumsum(rnorm(n)),type="l",main="The stock market",ylab="Money",xlab="Time")
}
@
This is what the stock market looks like over the past month:
\begin{center}
<<fig=T,echo=F,width=10,height=4>>=
myplot(10)
@
\end{center}
and over the past decade:
\begin{center}
<<fig=T,height=4>>=
myplot(1000)
@
\end{center}
\end{document}

Knitr版本的块头略有不同:

<<echo=F,fig.width=10,fig.height=4>>=

输出看起来像这样:

example output

在两种情况下,绘图的宽度和高度均在源文件中进行硬编码。在我看来,这就像不良的软件设计。我希望在函数myplot()中指定图的宽度和高度。否则,如果我想更改绘图的外观,或者要从信纸转换为A4纸,那么我将承担繁琐的任务,即使用搜索和替换来更改几个硬编码的宽度和高度值。

在Knitr中,可以通过R代码更改认的宽度和高度:

<<cache=FALSE>>=
opts_chunk$set(fig.width=3,fig.height=4);
@

但是,此配置必须与执行绘图的代码块分开,因为Knitr(如Sweave)在评估任何块代码之前会打开绘图设备。我需要的是一种在myplot()中指定高度和宽度的方法

在Sweave或Knitr中指定绘图宽度和高度以及绘图的其余部分的优雅方法是什么?

解决方法

这是我想出的一个新示例,它试图进一步说明对地块大小进行程序控制的实际需求。在使用编织器的此示例中,myplot将图写入PDF文件,然后使用asis_output发出引用同一PDF文件的\includegraphics语句。该语句包括一个width参数,该参数是根据PDF的尺寸计算得出的,因此,两个图使用相同大小的元素进行渲染。

\documentclass{article}
\title{knitr example with variable plot width using asis\_output}
\begin{document}
\maketitle
\begin{center}
<<echo=F>>=
library("graphics")
myplot=function(n,trim=0.4) {
  fn=sprintf("myplot-%d.pdf",n)
  nds=letters[1:n]
  ijs=expand.grid(i=1:n,j=1:n)
  edges=matrix(nrow = length(nds),ncol= length(nds),ijs$i == ijs$j+1)
  arrowlabels=ifelse(edges,"","_"); # "_" == absent,below
  width=n+1
  height=2
  pdfwidth=width-2*trim
  pdfheight=height-2*trim
  # plotmat expects coordinates in [0,1]
  repos=function(pos) {
    cbind((pos[,1]-trim)/pdfwidth,(pos[,2]-trim)/pdfheight)
  }
  pos=repos(cbind(1:n,1));
  pdf(fn,height=pdfheight,width=pdfwidth);
  par(mai=0*(1:4))
  plotmat(arrowlabels,pos=pos,name=nds,box.size=0.3/pdfwidth,curve=0,arr.lwd=1,arr.type="simple",arr.length=0.5,arr.pos=0.68,box.type="circle",box.col="white",shadow.size=0,absent="_",txt.font=3 # italic
  )
  dev.off();
  renderwidth=sprintf("%0.2f\\textwidth",pdfwidth/8);
  asis_output(paste0("\\fbox{\\includegraphics[width=",renderwidth,"]{",fn,"}}"))
}
@
\end{center}

\begin{center}
  \noindent A linked list could have as many as four elements: \\
\Sexpr{myplot(4)} \\
or even seven: \\
\Sexpr{myplot(7)}
\end{center}

\end{document}

输出:

example output

我进行了以下更改以使其与Sweave一起使用。 Sweave版本不够简洁,因为我不知道如何在\Sexpr模式下调用results=tex

   dev.off();
   renderwidth=sprintf("%0.2f\\textwidth",pdfwidth/8);
-  asis_output(paste0("\\fbox{\\includegraphics[width=","}}"))
+  cat(paste0("\\fbox{\\includegraphics[width=","}}"))
 }
 @
 \end{center}

 \begin{center}
   \noindent A linked list could have as many as four elements: \\
-\Sexpr{myplot(4)} \\
+<<results=tex,echo=F>>=
+  myplot(4)
+@
+\\
 or even seven: \\
-\Sexpr{myplot(7)}
+<<results=tex,echo=F>>=
+  myplot(7)
+@
+\\
 \end{center}

 \end{document}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?