Skip to content

print to .jpeg

5 messages · Benjamin Caldwell, Luke Miller, Henrik Bengtsson

#
Hi,

First, don't use JPEG for your scientific plots - the image file
format uses a compression that is really bad for anything but photos.
Instead use PNGs. For scientific plots, PNG files are often also
smaller than JPEG files, e.g. in your case the JPEG is ~4 times larger
than the PNG.  See also
http://www.labnol.org/software/tutorials/jpeg-vs-png-image-quality-or-bandwidth/5385/

Next, as already explained, you have to create your own filename
string that you pass to png() (or jpeg() is you still insist on using
that), e.g.

species.name <- "CussoniaHolstii"
filename <- sprintf("%s.png", species.name)
png(filename)
dia <- 10:100
biomass <- -21.4863 + 0.5797 * (dia ^ 2)
plot (biomass, main=species.name, xlab="dbh in cm", ylab="biomass in kg")
dev.off();

Alternatively, use the devEval() function of the R.utils package.
Then the filename extensions is automatically added and the device
automatically closed afterwards, e.g.

library("R.utils");

devEval("png", name=species.name, {
 dia <- 10:100
 biomass <- -21.4863 + 0.5797 * (dia ^ 2)
 plot (biomass, main=species.name, xlab="dbh in cm", ylab="biomass in kg")
})

Use "jpeg" instead of "png" to get a JPEG file, "pdf" to get a PDF and
so on.  Image files are by default written to the figures/ directory
(which is created if missing).  The default directory can be set by
setOption("devEval/args/path", "myImages").

/Henrik
On Tue, Apr 12, 2011 at 7:03 PM, Luke Miller <millerlp at gmail.com> wrote: