R Graph Cannot Read Text Too Big
So you've spent hours slaving over the code for a beautiful statistical graphic in R, and now y'all're fix to testify it to the world. You might be press information technology, embedding it in a document, or displaying it on the web. Don't do your graph a disservice by causing it to look anything less than perfect in its final venue. Here are 10 tips to help make sure your graphic will ever look best.
1. Phone call the right device driver from a script
It's tempting to just create graphics to the on-screen device (such as X11 on Linux or Quartz on MacOS) and then just use "Relieve As..." from the menu. However, this doesn't allow you to explicitly set the options for the device, and on some platforms, y'all don't even get to choose the file format. Likewise, if yous resize the graphics window after you create the graph, you can get some unexpected results (such equally circles that look like ovals). Avoid using dev.copy for the same reason, despite its convenience.
The best practice is to create a script file that begins with a phone call to the device driver (usually pdf or png), runs the graphics commands, and then finishes with a phone call to dev.off(). For instance:
png(file= "mygraphic.png" ,width= 400 ,height= 350 )
plot(x=rnorm( ten ),y=rnorm( ten ),main= "example" )
dev.off()
Non simply volition you frequently get improve-looking results, only you'll have the means to recreate the graphic file six months downwards the line, when you've long forgotten how you did it manually.
two. If you're printing, use PDF
If yous program to print your graphic, you want to employ a vector-based format. This means that the graphic is represented in a calibration-independent format, and it can exist recreated in whatever size small or large without resulting in jagged lines or pixellated text. When you impress it on a printer, lines will appear smooth and text will exist clear, even if the graphic has been enlarged or reduced and regardless of the DPI (dots-per-inch) rating of the printer.
PDF (via the pdf() commuter) is the best choice: because PDF viewers are ubiquitous these days, your graphic can hands be viewed on Windows, MacOS and Linux machines. Information technology'south also piece of cake to create a high-quality printout of a PDF file on nigh any printer.
PDF is besides the best choice whenever yous want to send the graph as a file via email, and the recipient needs the best quality possible.
iii. For Web brandish, use PNG
PDF files aren't conveniently embedded in Web pages, so you'll need to use a pixel-based format instead. GIF was the most popular format for many years, but information technology has several limitations (not least, graphs using many colors -- similar image plots -- might non look right in GIF format). These days, the best choice is the PNG format, generated by the png() commuter. Most browsers these days tin display PNG graphics without problem.
The main choice you need to brand when using png() is the dimensions of the graphic in pixels. (This is specified with the width= and height= arguments to png). The choice of the 10 dimension is the almost of import: ideally, you desire the whole graph to fit on the screen at one time, and you definitely don't want the viewer to have to coil horizontally to encounter your whole graph in all its beauty.
Nearly every display is more than 800 pixels wide these days, and then width=800 is a expert option for a full-screen graphic. If your graph needs to fit into a column (for a web log-entry, say), you might want to cut that downwardly to 400 pixels. Choose the Y dimension based on your desired attribute ratio (run into #6, below) -- for well-nigh purposes I find choosing a slightly smaller Y-dimension (almost 85-ninety% of X) works well.
If you're not sure in advance how large the graphic will be on the Web page, a unproblematic trick is to create it at high resolution (more than than 1200 pixels in either management), and employ the height= OR width= options (but not both, to preserve aspect ratio) for the img tag in HTML to shrink information technology downward to size. This can make your folio slower to load than necessary, just most browsers these days are good at preserving paradigm quality when resizing images. (Run across #5 for some caveats when generating high-resolution PNG files.)
Remember, though: the lower the brandish resolution, the fewer fine details will exist visible on the final graph. Some graphics just have to be displayed big for the full consequence.
4. For documents or for detail, go hi-resolution
If you're inserting a graphic into a document like Word or Powerpoint, a vector format like PDF would in theory be the best, since it's independent of calibration. In practice though, Microsoft products don't handle embedded vector graphics reliably: with some attempt y'all can make it look OK when printed, but it can be a hurting to edit or review a certificate that includes a vector graphic. (Open up-source LaTeX handles this much better, where embedded PostScript is the best choice.)
In this situation the best compromise is to stick with PNG as with the Spider web example, simply at MUCH college resolution. In Word yous can resize the graphic to an advisable size, but the high resolution gives y'all the flexibility to choose a size while non compromising on the quality. I'd recommend at least 1200 pixels on the longest side for standard printers. If your graphic is being professionally printed (in a book or on a poster, for example), check with your print store for their recommendations (they'll probably want a PostScript file or a very high-resolution TIFF file).
5. Choose your dimensions carefully
R always has a concept of the real-earth dimensions of your graphic measured in inches, independent of the number of pixels used to render a PNG or the actual size a PDF may exist enlarged or reduced to when printing. The choice of physical dimensions is important whenever you utilize text on graph -- which is almost always, since tick labels, axis labels and titles are all examples of text.
R uses the number of graph inches on the X and Y axis to determine the actual width and meridian of messages drawn on the page. As a general dominion, as the graph size in inches gets large, the size of the text relative to the graphic gets smaller; conversely, for smaller graphics the text gets large relative to the graph elements. You tin correct for this using the cex option to the text-plotting commands, only this gets existent fiddly real fast.
For PDF graphs this is easiest to bargain with, where you specify width and summit in inches anyway. Even if yous plan to brandish your graph on a huge poster, it's all-time to stick with homo-scale dimensions of seven-x inches per side. This is a size that would fit comfortably when printed on Letter (Usa) or A4 (metric) paper. Since PDF is scalable, you can zoom up the graphic for whatever side yous need, and the text will stay at a comfy size relative to the information.
For PNG graphs, information technology'south a bit tricker. By default, R assumes 72 pixels to the inch, then when you lot increase the pixel dimensions you're besides increasing the implicit size of the graph area. Here'south an instance of a 400x350 graphic with the default settings:
png(file= "animals72.png" ,width= 400 ,pinnacle= 350 ,res= 72 )
plot ( Animals , log ="xy", blazon ="northward", main ="Animate being brain/body size")
text(Animals, lab=row.names(Animals))
dev.off()
R is assuming the graph area is 5.55 inches across, so the default text size is large relative to the graph itself. You tin can correct this with the res= argument to png, which specifies the number of pixels per inch. The smaller this number, the larger the plot surface area in inches, and the smaller the text relative to the graph itself. Let'southward see what happens when you driblet this down to 45/inch:
png(file= "animals45.png" ,width= 400 ,tiptop= 350 ,res= 45 )
plot ( Animals , log ="xy", type ="n", master ="Animal brain/body size")
text(Animals, lab=row.names(Animals))
dev.off()
Note the championship is smaller, and the text labels are smaller too, making for a less-crowded plot. I similar to choose a resolution that gives me an X dimension in the 8-10 inches range (hither 400/45 = 8.33 inches).
6. Think about aspect ratio
R's PDF graphics driver by default gives a 7x7inch square surface, and it's tempting to choose equal X and Y pixel dimensions for PNGs. But some graphs lend themselves to displays much wider than they are tall (like time series), and others look better as tall, thin graphs (lattice graphs, for example).
Consider the aspect ratio when choosing the dimensions of your PDF or PNG graphic, and choose an 10-top to Y-height ratio that serves the data best. Whatever you practise, don't stick with a square default and resize the graphic to a new attribute ratio for brandish. This volition result in stretched text elements and other unpleasant artifacts.
Also, remember that the graph dimensions you set up in the pdf or png call include all the outer margins around the graph itself, and by default they're not the same size on all sides. You'll either desire to adjust the graph size accordingly, or reset the margins as shown in the next tip.
7. Remove the outer margins, if you lot're not using them
R reserves infinite at the height of the graph for the championship, and space on the lesser and left side for the centrality labels. If your graph doesn't include any such labels, it's a adept thought to tell R to use this space for the graphic, instead. This makes it easier to embed your graph into a Spider web page or certificate without having to futz with clipping or spacing. It as well makes things a bit easier if you lot later accept to reproduce your graph in smaller dimensions, where the space reserved for the labels tin can take up a pregnant portion of the plot surface area.
To remove the space reserved for labels, utilize par(mar=...). For case
png(file= "notitle.png" ,width= 400 , height= 350 )
par(mar=c(5,3,two,two)+0.1)
hist(rnorm( 100 ),ylab= NULL ,master= Goose egg )
dev.off()
The four numbers in the telephone call to par are the number of lines of text reserved on the lesser, left, summit and right, respectively. The default is to leave 4.1 lines at the acme; in the example above I've reduced information technology to 2.1, which is a sensible minimum to get out a small buffer of whitespace around the graph. On the left side I reduced information technology to 3.1 (from a default of four.i), leaving enough room for the y-axis tick labels. This is the result (with a surrounding box to show the dimensions of the graph area):
Compare to the consequence using default margins by eliminating the call to par in the script above:
8. Brand sure anti-aliasing is enabled
When a diagonal line is displayed on a computer screen, the points on the line don't line up exactly with the rectangular filigree on the screen. This causes the line to look jagged, as if it'due south taking a series of steps upward (with each row of pixels on the screen) instead of smoothly ascending. This visual effect is alleviated with anti-aliasing, which uses automatically uses grey pixels where the line doesn't quite fill an on-screen pixel, lessening the "jaggie" outcome and mostly making lines, text, and other elements look smoother on the screen.
You don't need to worry well-nigh this with PDF graphics (the PDF viewer handles this for you), but information technology tin be an issue with PNG graphics. On most systems R volition use anti-aliasing automatically, simply on some (those without X11), information technology'due south not available. Here's a graphic (from the now-defunct R graph gallery) created without anti-aliasing:
Antialiasing is enabled on my system, then here's what information technology looks similar when I recreate it (typo and all):
In this version, the text is much easier to read and the lines appear smoother.
If you don't have anti-aliasing on your system (and tin't recompile R to enable it), yous tin use the poor-human being'due south anti-aliasing trick: generate the graph in double the resolution, and brandish it at one-half the size. The browser will handle the anti-aliasing, at the expense of boosted bandwidth for your graphic.
9. Don't utilize JPEG, ever
You might be tempted to apply the JPEG (aka .JPG) graphics format for the concluding product on the Web, but this is almost certainly a bad idea. JPEG works fine for photograph-like images, but introduces blurry artifacts around lines and letters for the typical R graph. You might relieve a few kilobytes in the file size by using the jpeg device driver or converting your PNG into .JPG, but only at a significant expense in quality.
10. Be creative
Of course, the most important tip for making your graph look skilful is: make a practiced-looking graph! Graphical display of quantitative data is in some ways more than art than science, but every bit a general dominion information technology takes time and effort to make a truly effective display that lets your data tell the story it needs to tell. Fortunately, R provides you with all the tools you demand to pull out all the details, make the correct comparisons, and make the results pleasing to the middle. Don't be satisfied with the "stock" graphs from the height-level functions like plot or hist. Make liberal use of the annotation functions like text and line, and experiment with choices of color, layout, and size.
There are many good resources for learning nigh making adept graphical displays, but my favorite is Tufte'due south classic: The Visual Display of Quantitative Information. Not only is it chock-full with wonderful examples and sensible guidelines for displaying data, it makes a beautiful coffee-table volume to prove your non-statistician friends that Statistics is about more than than just numbers.
If you lot want to download the scripts that generated the graphs in this article, you can get them here:
Download graphexamples.R (1.4K)
That'southward information technology! Do you lot have any other tips for making expert graphics? Let us know in the comments.
The comments to this entry are closed.
Source: https://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html
0 Response to "R Graph Cannot Read Text Too Big"
Post a Comment