Figure numbers generated by Sweave/R and why only (PDF)LaTeX

I am continuing my earlier post here:

Beginner's questions (figures, bibliography) with Sweave/R/LaTeX---my first document

The working code is reproduced here:

\documentclass[a4paper]{article}
\usepackage{Sweave}  %%%%%%
\begin{document}
<<echo=TRUE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@

<<echo=FALSE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@

<<echo=TRUE>>= 
##### Remove all comments from your data file 
test.frame<-read.table(file="apples.d",header=T,sep= "")
names(test.frame)
head(test.frame)
class(test.frame)
@

\begin{figure}[htbp]
\begin{center}
\setkeys{Gin}{width=0.5\textwidth}
<<echo=FALSE,fig=TRUE,width=4,height=4>>=
#### Must tell plot where to get the data from. Could also use test.frame$year
with(test.frame,plot(year,value))
@
\end{center}
\end{figure}
\end{document}

The above runs fine with RStudio (latest) and Tinn-R (latest) and the desired pdf document is produced.

Questions:

  1. If I name the above file as goodex.snw and I run Sweave, I get the file goodex-004.pdf with either Tinn-R or RStudio as the PDF image of the plot. Why the trailing 004? Can this be changed?

  2. Can an EPS file be produced? Is the tool by which Sweave compiles to PDF is only through (PDF)LaTeX and not through the traditional DVI > PS > PDF route?

  3. Just running the command with(test.frame,plot(year,value)) in the R command window generates more values on the y-axis i.e. 15000, 20000, 25000 and 30000. However in the PDF file produced by Sweave by my code at the top of this post, I do not get all the values on the y-axis (only 15000 and 25000). How to control the size of the plot directly in the code so that all necessary y values appear?


Update: the file apples.d contains:

#Number of apples I ate
year value
8   12050  #year 2008  
9   15292  #year 2009 
10  23907  #year 2010 
11  33997  #year 2011

Answers


Your example is not reproducible because we don't have the file apples.d, so we can only guess why the plot goes wrong. Please see:

How to make a great R reproducible example?

on how to make a reproducible example.

Please note that Sweave is not a functionality of Rstudio or Tinn-R, it is an R function (Sweave()) that can be run from command line or with arguments from the R executable. This might be helpful to know if you are searching for information.

As for your questions:

  1. The names of plots always have the form FILENAME-CHUNKLABEL.pdf or eps, where the chunk label can be set as option to the Sweave chunk (it's the first argument). If you don't set a chunk name the plots will be enumerated.
  2. You can use eps with the option eps=true. I am pretty sure that by default both eps and pdf are produced though. As for compiling, Sweave does not compile by itself, it creates a .tex file that you can use in whatever way you want. In R 2.14 there is an option to run pdfLaTeX on the produced .tex file automatically,. The way Rstudio and Tinn-R compile is probably by using an pdfLaTeX call after Sweave. You can do it manually if you want to do it differently.
  3. Without a reproducible example we can only guess. What is going wrong? You could set the limits of the plot with the xlim and ylim arguments but that shouldn't be what is going wrong here.

Edit:

In response to edited question with data. First just a tip. This way of giving the data isn't the most useful way of doing it. We can of course reproduce this but it is much easier if you give the data in a way we can run immediately. e.g.:

test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))

As for the plot, you mean the labels on the y axis? This can be changed by omiting axes in the plot call and setting them manually with the axis() function:

with(test.frame,plot(year,value,axes=FALSE))
axis(1)
axis(2,test.frame$value,las=1)

This does look a bit weird if the ticks aren't constantly distributed over the axis though. Better would be:

with(test.frame,plot(year,value,axes=FALSE))
axis(1)
axis(2,seq(10000,35000,by=5000),las=1)

Need Your Help

How would I initialize an array of 'int' as a class member in c++11?

c++ arrays c++11 integer

Okay, A simple question, but I CANNOT find the answer I had before...

Find string from responsetext

javascript xmlhttprequest

In the responetext is the entire source of the page, but if the page contains a link with the word tuna, how can I grab the link from responsetext?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.