кеш рисунков ggplot2 с knitr - PullRequest
       20

кеш рисунков ggplot2 с knitr

4 голосов
/ 24 февраля 2012

У меня есть следующий код, который работает как есть, но не работает, когда кеш = T Смена устройства не имеет значения (по умолчанию, tikz, cairo)

% \SweaveOpts{fig.path=cache/figure/plot-,cache.path=cache/data/data-,fig.align=center,external=TRUE,fig.show=hold,cache=TRUE,echo=FALSE,pdfcrop=TRUE}

<<message=F,fig.width=9,fig.height=6,out.width=\textwidth,cache=F>>=
grid.newpage()
pushViewport(viewport(layout = grid.layout(2,9))) 

d <- ncol(rTSc)
p <- ggplot(melt(coveig),aes(1:d,value,group=variable,col=variable))  + 
  geom_line() + labs(x="index",y="eigenvalue") + 
  opts(legend.position = "none")  
print(p, vp=viewport(layout.pos.row=1,layout.pos.col=1:4))
p <- ggplot(melt(coreig),aes(1:d,value,group=variable,col=variable)) + 
  geom_line() + labs(x="index",y="eigenvalue")
print(p, vp=viewport(layout.pos.row=1,layout.pos.col=5:9))

p <- ggplot(melt(coveig.cs),aes(1:d,value,group=variable,col=variable)) + 
  geom_line() + labs(x="index",y="cumulative eigenvalue") + 
  opts(legend.position = "none")
print(p, vp=viewport(layout.pos.row=2,layout.pos.col=1:4))
p <- ggplot(melt(coreig.cs),aes(1:d,value,group=variable,col=variable)) + 
  geom_line() + labs(x="index",y="cumulative eigenvalue")
print(p, vp=viewport(layout.pos.row=2,layout.pos.col=5:9))
@ 

Почему это так? Есть идеи?

Спасибо!

1 Ответ

5 голосов
/ 25 февраля 2012

Похоже, переопределение p - это то, что заполняет кэш. Попробуйте сохранить изображения в виде отдельных графиков, а затем перенести их в окно просмотра по одному. (Это также делает для более ясного кода).

\documentclass[12pt]{article}
\title{Example}

\begin{document}

<<loading,echo=F>>=
library(ggplot2)
library(gridExtra)
@

\section{This is a Section}

<<message=F,fig.width=9,fig.height=6,out.width=\textwidth,cache=T>>=
x <- rnorm(100)
y <- runif(100)
dat <- data.frame(x,y)

grid.newpage()
pushViewport(viewport(layout = grid.layout(2,9))) 

p1 <- ggplot(dat, aes(x,y)) + geom_point()
p2 <- ggplot(dat, aes(y,x)) + geom_point()

print(p1, vp=viewport(layout.pos.row=1,layout.pos.col=1:9))
print(p2, vp=viewport(layout.pos.row=2,layout.pos.col=1:9))
@

\end{document}
...