R Programming

Graphic Systems in R

If you want to plot a graph in R for your own consumption, it is pretty easy and you can generally do it with a few lines of code. However, if you are creating the graph for others consumption and want full control over all aspects of the graph, it will require some effort. The standard R distribution comes prepackages with powerful graphing capabilities. There are three in-built graphic packages, namely, Base, Grid and Lattice graphic packages. Apart from these three, there is also ggplot2, another popular graphic package for R.

Base Graphics

Base graphics is the oldest and original graphic system built for R. This is the core plotting system and contains functions including plot()hist()boxplot() and many others. The code for base graphics is in the graphics package, which is loaded by default when you start R.

Grid

Grid is an alternative graphic system which was added to R at a later stage. It implements a completely different graphic system independent of the core graphics package. It is more like a framework and doesn't produce complete graphics by itself. For this reason, we seldom call functions from the grid package directly.

Lattice

Lattice is an add-on graphic package that contains code for producing Trellis graphics - graphs that display a variable or the relationship between variables, conditioned on one or more other variables. It includes functions like xyplot()bwplot()levelplot()histogram(). Lattice graphics in R make use of grid graphics. This is incompatible with Base graphics package.

ggplot2

ggplot2 is a system for 'declaratively' creating graphics, based on "The Grammar of Graphics". You provide the data, tell 'ggplot2' how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. Here is the statement of purpose for the ggplo2 package - ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts. It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics.

Base graphics are used most commonly and are a very powerful system for creating 2-D graphics. In this course, we will focus on the Base graphics system and ggplot2.

Finance Train Subscription

Unlock full access to Finance Train and see the entire library of member-only content and resources.