views:

88

answers:

2

Hello,

I want to create a graphics of a function in R. The code is:

 x <- seq(from=0, to=1, by=0.00001)
 f <- function(x) ....
 y <- f(x)
 plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5)
 min(y)
 [1] 0.2291203
 max(y)
 [1] 0.7708797
  1. When I save the graphics as bmp from RGui, it looks like here and this is fine. When I save it as eps and include in LaTeX with:

    \begin{figure}[htbp]
    \centering
        \includegraphics[scale=0.4]{./images/f-probart.eps}
    \end{figure}
    

    it is skewed, as shown in the screen capture from here What is wrong? I guess there is a problem with exporting from RGui in eps, as the resulted eps is also shown skewed in IrfanView. Hence I suspect that is not the LaTeX inclusion code that is wrong...

  2. How could I create this graphics with a requested resolution, say 244 dpi? Is there another package/function that allows me to export eps with a specific resolution?

Thanks

+2  A: 

I cannot reproduce your error, so I guess it's something specific to your system. If I save as eps and include it in latex (using the graphicx package), everything works completely fine. Keep in mind that if you used the postscript() function in R, you have to specify the width and height of your picture as well. I could be wrong, but I think it defaults to the default values of the graphics window in R (which could explain the dimensions of your eps pictures).

If you saved from the graphics window, it normally should take the current width and height of the graphics window. It does so on my R version, but maybe your options are set differently? check ps.options() and see if width and height have value 0. If that's not the case, that could be the problem.

On a side-note : You could use pdf instead. See ?pdf in R. It allows you to specify the width and height of the picture, and reproduces correctly in Latex. You should use pdftex for building the file then.

My experience is that using pdf graphics and pdftex is less trouble than passing through PS. In fact, in latex there is no need to pass through eps any more to come to a decent pdf. Another advantage of using pdftex is that you can easily combine all graphics formats in the same document. (For EPS you need the epstopdf package)

2) the dpi requirement is only useful for grid images, so not for eps and pdf which are vectorized. I'd use png, that's the best format for graphs. See the option res in the function png().

 png("somefile.png",res=244)
 plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5)
 dev.off()

Alternatively, you could use the function bmp() for bitmap graphics in exactly the same way. Don't forget the dev.off() at the end.

Joris Meys
What version of R are you using? What Operating System? mine: R 2.11.1/Windows 7 x32.
lmsasu
R2.11.1 32 bit on a Windows 7 64 bit. But this is exactly the same in Vista and XP, and R2.10.x Did you check the ps.options() already?
Joris Meys
In ps.options(), both width and height are 0.
lmsasu
pdf instead ps: unfortunately, I am not sure this is an option for me.
lmsasu
+1  A: 

I used the Cairo package; the code was:

    Cairo(24000,24000,file="a.ps",type="ps",bg="transparent",pointsize=12, units="px", dpi=2400)
    plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5, type='l')
    dev.off()

The resulted graph looked fine. One question, however: according to @Joris Meys, the dpi is useless for vector graphics; in this case, why specifying dpi for the Cairo function is mandatory?

lmsasu