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:
RColorBrewer Palettes
Color Groups
RColorBrewer is an R packages that uses the work from http://colorbrewer2.org/ 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
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.
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.
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
1 2 3 4 5 6 7 8 |
x <- 1:10 y <- c(4, 3, 2, 5, 6, 7, 1, 8, 9, 7) plot(x, y, type = "b", main = "Simple Plot", xlab = "X-axis Title", ylab = "Y-axis Title", col = "red3") |
Bar Plot of Vector
1 2 3 4 5 6 7 8 |
# Bar plot of vector set.seed(1) y.vec <- sample(-10:10,10, rep = TRUE) barplot(y.vec, col = "steelblue4", names = LETTERS[1:10], ylim=c(-10,10)) abline(h = 0) |
Bar Plot of Matrix: #1
1 2 3 4 5 6 7 8 |
# Bar plot of matrix: #1 set.seed(10) y.mat <- matrix(sample(1:10, 60, rep = TRUE), nrow = 6) barplot(y.mat, col = colorRampPalette(c("steelblue4", "green"))(6), names=LETTERS[1:10], ylim=c(0, 50)) abline(h = 0) |
Bar Plot of Matrix: #2
1 2 3 4 5 6 7 8 |
# Bar plot of matrix: #2 barplot(y.mat, col=colorRampPalette(c("steelblue4", "green"))(6), names=LETTERS[1:10], horiz = TRUE, legend = paste("Sensor", 1:6), xlim=c(0,50)) abline(v = 0) |
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.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Configure graphical device par(mai = c(0.3, 0, 0.3, 0), bg = "grey85") h.grid <- layout(matrix(c(1, 2, 3, 4, 5), nrow = 5)) h.count <- c(3, 6, 12, 24, 48) # Plot creation for(i in 5:1){ h.ramp <- heat.colors(h.count[i]) barplot(rep(2, h.count[i]), axes = FALSE, space = 0, col = h.ramp, main = paste0("n = ", h.count[i])) } # Reset graphical device to default values par(op) |
Alpha Transparency
Heat color transparency is controlled with the optional alpha argument. The default value is alpha=1.
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.
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.
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. layout.show() can then be used to see multi-graph layouts and how the graphical device is being split.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# Multiplot creates a layout structure for multiple plots in one # window. The function accepts ggplot objects via "..." or as a # list of objects. Use input "cols" to define the number of # columns in the plot layout. Alternatively, input "layout" to # define the row/col matrix of plot elements. If "layout" is # present, "cols" is ignored. For example, given a layout defined # by matrix(c(1,2,3,3), nrow=2, byrow=TRUE), then plot #1 goes in # the upper left, #2 goes in the upper right, and #3 will cross # the entire bottom row, utilizing both columns. multiplot <- function(..., plotlist = NULL, file, cols = 1, layout = NULL) { # launch the grid graphical system require(grid) # make a list from the "..." arguments and/or plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # if layout = NULL, then use 'cols' to determine layout if (is.null(layout)) { # make the panel using ncol; nrow is calculated from ncol layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols)) } if (numPlots==1) { print(plots[[1]]) } else { # set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # put each plot, in the correct location for (i in 1:numPlots) { # get the i,j matrix position of the subplot matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } } |
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.
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
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.
Term Structure of Crude Oil 2018
An animation showing the term structure of NYMEX crude oil. For source code, go here.