Re: [R] print to .jpeg

From: Henrik Bengtsson <hb_at_biostat.ucsf.edu>
Date: Tue, 12 Apr 2011 20:44:40 -0700

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:
> And of course I need to close the parentheses completely on jpeg().
> Apologies for the double post.
>
> jpeg(paste(species.name, '.jpg', sep = ''))
>
> On Tue, Apr 12, 2011 at 10:02 PM, Luke Miller <millerlp_at_gmail.com> wrote:
>
>> How about using paste() inside the jpeg() function to append a '.jpg' to
>> the end of your species name?
>> See the change below. I also added a dev.off() to close the newly created
>> jpeg.
>>
>>
>> species.name="CussoniaHolstii"
>> dia<-10:100
>> biomass = -21.4863 + 0.5797 * (dia ^ 2)
>> biomass
>> jpeg(paste(species.name, '.jpg', sep = ''))
>>
>> plot (biomass, main=species.name, xlab="dbh in cm", ylab="biomass in kg")
>> dev.off()
>>
>>
>> On Tue, Apr 12, 2011 at 9:54 PM, Benjamin Caldwell <
>> btcaldwell_at_berkeley.edu> wrote:
>>
>>> Evening folks,
>>>
>>> I'm trying to print a series of graphs to .jpeg using a variable as the
>>> title, but run into the difficultly that I can't find a way to append the
>>> file extension to the .jpeg (in this case extensionless!) files.
>>>
>>> Example:
>>> ----
>>> species.name="CussoniaHolstii"
>>> dia<-10:100
>>> biomass = -21.4863 + 0.5797 * (dia ^ 2)
>>> biomass
>>> jpeg(species.name)
>>> plot (biomass, main=species.name, xlab="dbh in cm", ylab="biomass in kg")
>>> -----
>>> The output is CussoniaHolstii, but I want CussoniaHolstii.jpg. The help
>>> file
>>> for jpeg() specifies that the name include the extension (e.g.
>>> jpeg("CussoniaHolstii.jpg") but then I'd have to input the file name each
>>> time.
>>>
>>> Any help or workaround much appreciated.
>>> *
>>> *
>>> *Ben Caldwell*
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help_at_r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> ___________________________
>> Luke Miller
>> Postdoctoral Researcher
>> Marine Science Center
>> Northeastern University
>> Nahant, MA
>> (781) 581-7370 x318
>>
>>
>>
>
>
> --
> ___________________________
> Luke Miller
> Postdoctoral Researcher
> Marine Science Center
> Northeastern University
> Nahant, MA
> (781) 581-7370 x318
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 13 Apr 2011 - 03:48:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 13 Apr 2011 - 04:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive