Question

I am still learning Sweave and R. I have below a sample code that reads in a data file and plots it. I am choosing the postscript option as I like ending up with an EPS file. There are a number of things that I would like to improve with the plot. Here is my code with my own comments for my own learning:

\documentclass[a4paper,12pt]{article}
\usepackage{Sweave}  %%%%%%
\SweaveOpts{eps=TRUE}

\begin{document}

<<echo=FALSE, results=hide>>=
test.frame<-data.frame(ratio= c(0.0, 144.321, 159.407, 178.413, 202.557), value= c(0, 0.84, 0.8925, 0.945, 0.9975))
@


<<echo=FALSE,results=hide,eval=TRUE>>=
postscript('doudou.eps',
               width=7, height=6,
               colormodel="cmyk",
               family = "ComputerModern",
               horizontal = FALSE,
               onefile=FALSE,
               paper = "special",
               encoding = "TeXtext.enc",
               pagecentre=FALSE)

with(test.frame,plot(ratio, value, ylab= "Hello",
                                   xlab="Wonderful",
                                   type="o",        # line and markers
                                   bty="o",         # box around graph
                                   lty="solid",     # solid line or put 1
                                   lwd=3,            # line width
                                   pch=1,            # or enclose symbol in quotes
                                   cex=3,             # size of markers
                                   cex.lab=2,        # label size
                                   cex.axis=1.5,    # axis annot size problem if big
                                   cex.main=2,          # main title size
                                   xaxp=c(0, 200, 4),  #c(x1, x2, n)
                                   col=2,              # plotting color
                                   xlim=c(0,200),
                                   yaxt = "n",         #suppresses axis
                                   main=" My curve"))

axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.5,cex.main=2)

dev.off()
@

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.8\textwidth]{doudou.eps}
\end{center}
\end{figure}


\end{document}

Some things that I would like to know more about an improve:

  1. I have a boxed frame around the plot. How to control its line width?

  2. I am using cex.axis=1.5for the axis annotation size. if I change it to say cex.axis=3 then the values on the x-axis get big and they overlap with the tickmarks. Is there a way to then place the x-axis values a bit lower further away from the plot?

  3. The y-label Hello is truncated at the top of the H letter in the plot. How to fix this?

  4. How to move either the x-label Wonderful or y-label Hello further away from the plot?

  5. If we look at the plotted curve, the axes do not start at (0,0) though the initial values are (0,0) for the data set. How to control the axes so that they start at (0,0)?

Thanks a lot...

Was it helpful?

Solution

"I have a boxed frame around the plot. How to control its line width?"

 box(lwd=3)

"I am using cex.axis=1.5 for the axis annotation size. If I change it to say cex.axis=3 , then the values on the x-axis get big and they overlap with the tickmarks. Is there a way to then place the x-axis values a bit lower further away from the plot?"

par(mgp=c(3,1.5,0) )  # second element is number of lines below the box for the labels

"The y-label Hello is truncated at the top of the H letter in the plot. How to fix this?"

# use par() to increase left margins

"How to move either the x-label Wonderful or y-label Hello further away from the plot?"

par( mgp=c(4,1.5,0) ) # First element in mgp vector

"If we look at the plotted curve, the axes do not start at (0,0) though the initial values are (0,0) for the data set. How to control the axes so that they start at (0,0)?"

 ..., xaxs="i", yaxs="i", ... # can be done in `par` or in the plot call

So the R code for the figure is below: So the R code for the figure is below:

postscript('doudou.eps',
               width=7, height=6,
               colormodel="cmyk",
               family = "ComputerModern",
               horizontal = FALSE,
               onefile=FALSE,
               paper = "special",
               encoding = "TeXtext.enc",
               pagecentre=FALSE)
par( mgp=c(4,1.5,0), mai=c(1.5, 1.5, 1.5, .75) )  # using inches as the spacing unit
with(test.frame, plot(ratio, value, ylab= "Hello", 
                            xaxs="i", yaxs="i",
                                   xlab="Wonderful",
                                   type="o",        # line and markers
                                   bty="o",         # box around graph
                                   lty="solid",     # solid line or put 1
                                   lwd=3,            # line width
                                   pch=1,            # or enclose symbol in quotes
                                   cex=3,             # size of markers
                                   cex.lab=2,        # label size
                                   cex.axis=3,    # axis annot size problem if big
                                   cex.main=2,          # main title size
                                   xaxp=c(0, 200, 4),  #c(x1, x2, n)
                                   col=2,              # plotting color
                                   xlim=c(0,200),
                                   yaxt = "n",         #suppresses axis
                                   main=" My curve"))

axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.4, cex.main=2)
box(lwd=3)
dev.off()

Not pretty, but it does illustrate the control functions. Basically you need to spend more time on the help(par) page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top