Skip main navigation

Data Visualisation Packages and Principles – a Focus on ggplot2

Data visualization packages and principles - a focus on ggplot2
© Wellcome Genome Campus Advanced Courses and Scientific Conferences

As we saw, much of what is done with R can also be done in RStudio.

Data Visualization in RStudio

Introduction

As we saw, many things you do with R can also be done in RStudio. We would advise you to take some time to test in RStudio a few commands we used previously in R, and put in a Script what you want to use later. We would also recommend that you take some time to get familiarized with the RStudio display and test the options you can use through the user interface. This will be useful for the upcoming steps, where we will need to install packages, load them, use them, and generate plots using ggplot2.

Data visualization packages in R/RStudio

For the rest of the Steps, we will see how to use a dedicated package in RStudio for data visualization, called ggplot2.

Many packages exist that are devoted to data visualization.

The base graphics comes for example with the graphics package, and allows you to generate simple plots and then to improve aspects of the plot possibly through series of functions (we saw a quick example with “par()” and “plot()”).

The lattice plotting system is implemented with other packages such as the lattice package, which supports generating trellis graphs. It is generally used with a single function call that would specify all graphical parameters, which allow R to automatically compute the necessary graphical display.

With the ggplot2 package, we are almost combining both concepts. This package is based on using the grammar of graphics concept. It became so popular, that many other data visualization packages can complement it or use it, or are based on the same concepts. Examples of other packages are ggforce or ggvis.

It is also possible now to create more advanced interactive graphs in R/RStudio using packages such as Plotly or Shiny.

The grammar of graphics: basics of ggplot2

The grammar of graphics is the concept of using a particular grammar or language to specify and create certain statistical and graphical displays for data visualization. Applied to ggplot2, the grammar of graphics is implemented in a layered approach, using layers of information (data complemented with statistical or graphical information) to build up step by step to a final display of a graph. Layers complement each other in ggplot2 with different information types such as the aesthetics, the geometries, the faceting, the scales, the themes, the coordinates, the labels, and many others.

Here are explanations of some of these layers :

   
DATA The data you want to plot
AESTHETICS The graphical properties of data on the plot (x, y, …)
GEOMETRIES The graphical elements that determines the visual display of a plot (point, line, bar, area, …). Each geometric object is related to specific aesthetics. For example, a geometric object “point” is related to aesthetics shape, size, color and position
FACETING reorganizes the variables of a data into subsets with a certain graphical arrangement of elements
SCALES defines how data is mapped as related to aesthetics (item colors according to their class, …)
THEMES customizes non-data related display by defining options not directly related to the data itself
COORDINATES customizes the coordinates
LABELS sets plot title, legend or axis names

layers of grammar presented as parallel layers of different elements

Let’s explore these layers of information through examples in the next Step.

© Wellcome Genome Campus Advanced Courses and Scientific Conferences
This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education