How can I increase the resolution of my plot in R?

jake9115 picture jake9115 · Apr 2, 2014 · Viewed 17.4k times · Source

I am making simple plots in R and want the resulting images to be very high resolution. I tried just specifying large height and width parameters in the output image code, but this just makes a large image with fuzzy data points.

My current code looks like this:

png(filename="simple_graphic.png", width=5600, height=1000)
depth_plot <- c(a large list of data)
plot(depth_plot, type="o", col="blue", log="y",)
dev.off()

Any help optimizing this code to make the resolution of each data point higher is much appreciated!

Answer

r2evans picture r2evans · Apr 2, 2014

There are several possible answers to this question, and the comments are being careful to try to teach you where to find the answer vice just giving it to you. I'll spoil that by providing that to which they allude, but also present another question:

  1. The answer: use res=300 (or higher) in your call to png. From help(png), it states that the default ppi (pixels per inch) is 72, below what you want for fine-resolution graphs. 300 is decent for most printers and screens, YMMV.

    png(filename="simple_graphic.png", res=300) # perhaps width/height as well
    # ...
    dev.off()
    
  2. Another question: in what format can I best show my graph? This is often overlooked by many people making graphs with R, and is unfortunate but easy to remedy: use a vector-based image format. If you are using Powerpoint or Word, then you may be better off making an enhanced metafile format (EMF) file.

    install.packages('devEMF') # just once
    library(devEMF)
    emf(filename="simple_graphic.emf", width=1024, height=800)
    # ...
    dev.off()
    

    If you are able to use the image in a scalable vector graphic (SVG) format (e.g., Inkscape, Scribus, ...), then merely use svg(...) in place of emf(...). Same options/rules apply.

    Similarly, if you're LaTeX, you can go straight to pdf using pdf(...) (same syntax).

NB: one benefit of going to one of these vector-based (vice raster-based such as PNG) image formats is that if you want/need to finesse the graph in ways that you don't know how in R, you can open the EMF/SVG/PDF in something like Inkscape in order to easily modify lines (color, thickness), text (font, size, location), boxes, arrows, etc. And Inkscape will allow you to export into EMF, SVG, PDF, PNG, ...