使用knitr时如何旋转多个图块?

时间:2012-11-27 22:02:22

标签: r knitr

我正在使用knitr with plots。我想将其中一个旋转90度。例如:

\documentclass{article}

\begin{document}


<<cache=TRUE, echo=FALSE, message=FALSE, warning=FALSE, comment=NA, eval=TRUE, results=asis>>=

library("ggplot2")
library("gridExtra")

func <- function(data,x,y) {

  p1 <- ggplot(data.frame(data), aes(x = x, y = y)) + geom_point()
  p2 <- ggplot(data.frame(data), aes(x = x, y = y)) + geom_point()
  p3 <- ggplot(data.frame(data), aes(x = x, y = y)) + geom_point()
  p4 <- ggplot(data.frame(data), aes(x = x, y = y)) + geom_point()

  grid.newpage() 
    pushViewport(viewport(width = .9, height = .9,layout = grid.layout(nrow=2, ncol=2)))
    print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
    print(p2,vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
    print(p3,vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
    print(p4,vp = viewport(layout.pos.row = 2, layout.pos.col = 2))

  grid.newpage() 
    pushViewport(viewport(width = .8, height = .5,layout = grid.layout(nrow=1, ncol=2)))
    print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
    print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 2))

}

x <- runif(20,0,1)
y <- rnorm(20)
test <- cbind(x,y)
func(test,x=test$x,y=test$y)

@    

\end{document}

我想将第二页和图表都旋转90度。我知道我可以将它用于一页:

\usepackage{pdflscape}
\begin{landscape}
....
\end{landscape}

但这两个图由grid.newpage()在一个函数中排列。我怎样才能做到这一点?非常感谢!

1 个答案:

答案 0 :(得分:12)

我认为您可以在块选项中使用out.extra

来自knitr docs:http://yihui.name/knitr/options

  

out.extra :( NULL;字符)数字的额外选项,例如   LaTeX输出中的out.extra ='angle = 90'将图形旋转90°   度;它可以是任意字符串,例如你可以写多个   图选项中的选项;它也适用于HTML图像(额外的   选项将被写入标签,例如   out.extra = '风格= “显示:块;”')

<<out.extra='angle=90', cache=TRUE, echo=FALSE, message=FALSE, warning=FALSE, comment=NA, eval=TRUE, results='asis'>>=
# ... 
@