[R] howto improve sharpeness of fonts in a jpg-image produced by R ?

Marc Schwartz MSchwartz at medanalytics.com
Tue Nov 18 16:56:33 CET 2003


On Tue, 2003-11-18 at 09:04, Niels Steen Krogh wrote:
> I used an expression like this:
> 
> bitmap(file="barplotx.jpg",type="jpeg")
> barplot(c(1,2,3),c(3,4,5),main=list("Death Rates in Virginia", font = 1))
> dev.off()
> 
> 
> My question:  How can I improve the sharpeness of the text "Death Rates in 
> Virginia".
> Why jpeg: Its part of a web-environment using R through a SOAP-server. I 
> also tried png256, jpeggray and others, but the text "Death Rates in 
> Virginia" does not change sharpeness.
> 
> 
> /Niels

SNIP

As Prof. Ripley indicated, this is a Ghostscript (GS) issue, where R is
simply providing an interface of sorts to that application.

One option, at the expense of larger file and image size, is to increase
the dpi resolution for the image that GS generates, using the 'res'
argument in bitmap():

bitmap(file = "barplotx300.jpg", type = "jpeg", res = 300)
barplot(c(1, 2, 3), c(3, 4, 5), 
        main = list("Death Rates in Virginia",  font = 1))
dev.off()

You will likely get a better result (and a smaller file size) using a
PNG format file, if you must stay with a bitmapped approach:

bitmap(file = "barplotx300.png", type = "png256", res = 300)
barplot(c(1, 2, 3), c(3, 4, 5), 
        main = list("Death Rates in Virginia",  font = 1))
dev.off()

Keep in mind that PNG uses a lossless compression approach, whereas JPEG
will typically use a lossy compression, which results in a general
degradation of the image quality.

You can play around with the resolution settings until you get something
that is acceptable. The default is 72 which is fine for photographs in
browsers, but not for plots.

Keep in mind that the above will result in larger image dimensions using
the default height and width arguments. The above images will be 1800 x
1800 pixels, so you will need to scale accordingly. Notice however, that
even upon magnification, the fonts are still not as crisp as a vector
format image.

For example, try these and zoom in on the text:

pdf(file = "barplotx.pdf")
barplot(c(1, 2, 3), c(3, 4, 5), 
        main = list("Death Rates in Virginia", font = 1))
dev.off()

or this:

postscript(file = "barplotx.ps")
barplot(c(1, 2, 3), c(3, 4, 5), 
        main = list("Death Rates in Virginia", font = 1))
dev.off()

Both the PDF and PS formats will look even better printed.

HTH,

Marc Schwartz




More information about the R-help mailing list