Category Archives: R Graphics

Base Graphics in R

R graphics reference material is supplied on the high-level commands and the low level parameters that control full flexibility of R’s graphical environment.  Subchapters are organized as follows:

Back | Next

Posted in R Graphics | Comments Off on Base Graphics in R

RColorBrewer Palettes

Color Groups

RColorBrewer is an R packages that uses the work from to help you choose sensible colour schemes for figures in R.  The colors are split into three group, sequential, diverging, and qualitative. 

  • Sequential – Light colours for low data, dark for high data
  • Diverging –  Light colours for mid-range data, low and high contrasting dark colours
  • Qualitative – Colours designed to give maximum visual difference between classes
Posted in R Colors, R Graphics | Comments Off on RColorBrewer Palettes

Trellis Graphs


Back | Next

Posted in R Graphics | Comments Off on Trellis Graphs

Layered Plots

Layered plots are one way to achieve new insight and actionable intelligence when working with complex data.  ggplot is well suited for layered plots.

Data Pre-Processing

To make graphs with ggplot(), the data must be in a data frame and in “long” (as opposed to wide) format.  Converting between “wide” and “long” data formats is facilitated with the reshape2 package.  Specifically, the melt() function converts wide to long format, and the cast() function converts long to wide format.  The following code block presents examples of the two data formats.

Posted in ggplot2, R Graphics, R Programming | Comments Off on Layered Plots

R Graphics: Color Ramp Functions

Color ramp functions in R simplify color selection and assignment.  Visual reference examples are provided to illustrate their use and flexibility.

Color Ramp: rainbow()

The rainbow() ramp is is the standard light spectrum with red, green and blue as its base. The functional form is rainbow(n, s=1, v=1, start=0, end=max(1, n-1)/n, alpha=1).  The s and v values correspond to saturation and value from the HCL color scheme (described below).  The parameters start and end can be used to specify particular subranges of hues. The following values can be used when generating such a subrange: red = 0, yellow = 1/6, green = 2/6, cyan = 3/6, blue = 4/6 and magenta =5/6.

Posted in R Colors, R Graphics | Comments Off on R Graphics: Color Ramp Functions

R Graphics Gallery

An  R graphics gallery has been created with simple scripts for long-term reference and to expedite data visualization.

Simple Line Plot

Click to enlarge


Bar Plot of Vector

plot3          Click to enlarge

Bar Plot of Matrix: #1

plot4          Click to enlarge

Bar Plot of Matrix: #2

plot5          Click to enlarge

Posted in R Graphics | Comments Off on R Graphics Gallery

R Graphics: Plot Parameters

High level plot commands open a graphics device.  High level plot elements add objects.  R plot parameters ensure actual control over the graphics device.

R Plot Parameters

All high level plotting functions have arguments which can be used to customize the plot. barplot(), for example, has arguments to control bar width, styles, etc.  High level functions also take the optional “three dots” argument, which allows for argument sharing.  The most common arguments shared are low level graph parameters from the par() function.

Posted in R Graphics | Comments Off on R Graphics: Plot Parameters

R Graphics: Heat Colors

Heat colors in R utilize basic color ramp  functions.  Simple example scripts with basic control options follow.

Heat Colors

The heat.colors() function creates a ramp of contiguous colors clustered around the color orange in the red spectrum of the RGB scale.  The function has the form heat.colors(num_colors, alpha=value).

Color Ramp Example


Alpha Transparency

Heat color transparency is controlled with the optional alpha argument.  The default value is alpha=1.

Posted in R Colors, R Graphics | Comments Off on R Graphics: Heat Colors

R Graphics: Grey Scales

Grey scales in R can be used effectively in data visualization.  Several simple functions can be used to great effect.

Creating Gray Scales

The gray.colors() function creates a vector of interpolated values that represent evenly-spaced gray colors. 

The function has the form gray.colors(num_colors, start = value, end = value, gamma = value).  End and start define the endpoints of the range of grays, with 0 = black and 1 = white being the extreme values. (By default, start = 0.3 and end = 0.9.) gamma is an optional argument which controls luminance.

Posted in R Colors, R Graphics | Comments Off on R Graphics: Grey Scales

R Graphics: High Level Commands

High level commands and plotting functions in R set up a co-ordinate system.  No plotting is done inside a graphics device until at least one high level function has established the co-ordinate system.  High level commands include plot type and plot elements. Together, they provide users with direct control of all graph objects and support the creation of highly customized data visualizations.

Posted in R Graphics | Comments Off on R Graphics: High Level Commands

R Graphics: Multi-Graph Layouts

The layout() Function

The ability to manage multiple plots in one graphical device or window is a key capability to enhance data visualization and analysis.

The layout() function in base R is the most straightforward method to divide a graphical device into rows and columns.  The function requires an input matrix definition.  Column-widths and the row-heights can be defined using additional input arguments. can then be used to see multi-graph layouts and how the graphical device is being split.

Posted in R Graphics, R Programming | Comments Off on R Graphics: Multi-Graph Layouts

Multiple Plots (ggplot)

The multiplot() Function

Winston Chang’s R Graphical Cookbook provides a useful function to simplify the creation of layouts with multiple plots.  The function accepts ggplot objects as inputs.

Use of the function is straightforward.  First, multiplot() needs to be sourced and available in memory.  Then the plots need to be coded with variable assignments to create plot objects.  Finally, multiplot() is used to call the plot objects for placement in the predefined plot layout.

Posted in ggplot2, R Graphics | Comments Off on Multiple Plots (ggplot)

R Color Tables: Download

The following page provides R color tables by name and hexadecimal code. The tables can be downloaded for local reference or recreated with R code provided.

R Color Tables: By Name

A pdf file for the color name table can be downloaded here” R Colors By Name (2373 downloads)

Click to enlarge

Posted in R Colors, R Graphics | 1 Comment

Color Palettes in R

The following list details all color palettes and color ramp functions in R.  Detailed color tables are created for long-term reference and to aide code scripting.  The objective is to simplify the delivery of publication quality graphics.


Posted in R Colors, R Graphics | Comments Off on Color Palettes in R

Term Structure of Crude Oil 2018

An animation showing the term structure of NYMEX crude oil.  For source code, go here.

Posted in Animation, Data, Economics, R Graphics | Comments Off on Term Structure of Crude Oil 2018