Skip to content

Data Visualization Practices

Eric Marty edited this page Jan 11, 2018 · 13 revisions

General guidance for data visualization

Tufte, alberto cairo, other resources

R graph gallery

Figure layout

eric?

Data visualization in R

There are many ways to visualize data in R, but the two largest camps are using base R and ggplot. These two approaches use very different langauge in R, and so are generally not compatible with each other (e.g. you can't use a base R call to color within ggplot). Generally, it is best to choose whichever one you like and stick with it, becoming an expert in that method. Familiarity with base R plotting, however, is probably always a good idea.

Within the Drake lab, we have a fairly even split between both.

base R

ggplot

ggplot follows the conventions of Hadley Wikham's tidyverse, and so your data must be in long form to use it. It implements the grammar of graphics, which allows plots to be built in a layered format, with each element conforming to similar grammar.

all the gg extentension

Interactive visualizations

If you are doing exploratory analysis, it is sometimes helpful to be able to interact with your visualization to explore it. There are several R packages to help with this:

Plotly tends to be the most straightforward, unless you are trying to build a completely interactive site, which is what shiny is better suited for.

Saving figures in R

The best way to export figures is through a script and not via the 'Export' button on the viewer. A script allows you to control the resolution, size, file type, font, etc. much more easily than the viewer does. Learn more the call for each type of file type here.

In general, vector images (e.g. pdf, svg) allow for the highest resolution without too large of a file size. Most publications require at least 300 ppi (pixels per inch), often best saved as a tiff file, if the publication will not accept pdf.

Managing figures in git

If exported figures are included in a git repository, the exported images should be tracked with git-lfs (Git Large File Storage). "Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise." Follow these instructions to install git-lfs, add lfs to your repository, and specify which file types or folders to track with lfs. Thereafter, lfs works invisibly and you simply commit and push changes as usual.

Lab Links

Clone this wiki locally