Created
November 20, 2011 13:45
-
-
Save atomicules/1380278 to your computer and use it in GitHub Desktop.
Some Sweave file extracts for embedding in a blog post, showing different approaches for looping through Lattice plots for Sweave vs Tikz/pgfSweave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Reference for pgfSweave / Tikz option | |
%Sweave file extract | |
\begin{figure} | |
<<fig=TRUE, tikz=T>>= | |
print( | |
xyplot(Y ~ Date | Grouping, datatable[levels(datatable$Grouping)[1:8]], layout=c(1,8), ylim=c(-100,100), xlim=as.Date(c("2011-01-01", "2011-07-01")) ) | |
) | |
@ | |
\end{figure} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Tikz loop | |
%Sweave file extract | |
\usepackage{tikz} | |
%... | |
<<results=tex, echo=FALSE>>= | |
#Need to know how many levels | |
nl <- length(levels(datatable$Grouping)) | |
#then how many per graph | |
ng = 8 | |
#Can then loop | |
for (i in 1:ceiling(nl/ng)) { | |
cat("\\begin{figure}\n") | |
cat("\\begin{center}\n") | |
tikz(console=TRUE) | |
start=(i-1)*ng+1 | |
end=i*ng | |
print( | |
xyplot(Y ~ Date | Grouping, datatable[levels(datatable$Grouping)[start:end]], layout=c(1,ng), ylim=c(-100,100), xlim=as.Date(c("2011-01-01", "2011-07-01")) ) | |
) | |
dev.off() | |
cat("\\end{center}\n") | |
cat("\\end{figure}\n") | |
} | |
@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Traditional Sweave Loop | |
%Sweave file extract | |
<<eval=TRUE, echo=FALSE, results=hide>>= | |
#Cleanup - delete previous files | |
file.remove(list.files(pattern = glob2rx("ybygroup*.png"))) | |
#No need to use a loop here, lattice graphics will produce the necessary number of pngs | |
png(file="ybygroup%03d.png", width=1000, height=2000) | |
xyplot(Y ~ Date | Grouping, datatable, layout=c(1,8), ylim=c(-100,100), xlim=as.Date(c("2011-01-01", "2011-07-01")) ) | |
#This echos when using PNGs, so put in it's own block and hide. | |
dev.off() | |
@ | |
<<results=tex, echo=FALSE>>= | |
files <- list.files(pattern = glob2rx("ybygroup*.png")) | |
for(file in files){ | |
cat("\\begin{figure}") | |
cat("\\begin{center}") | |
cat("\\includegraphics{", file, "}\n\n", sep="") | |
cat("\\end{center}") | |
cat("\\end{figure}") | |
} | |
@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment