Question

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!

Was it helpful?

Solution

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, ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top