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.
Color Ramp Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Configure graphical device par(mai = c(0.3, 0, 0.3, 0), bg = "ivory") g.grid <- layout(matrix(c(1, 2, 3, 4, 5), nrow = 5)) g.count <- c(3, 6, 12, 24, 48) # Plot creation for(i in 5:1){ g.ramp <- gray.colors(g.count[i], start = 0, end = 1) barplot(rep(2, g.count[i]), axes = FALSE, space = 0, col = g.ramp, main = paste0("n = ", g.count[i])) } # Reset graphical device to default values par(op) |
Gamma Correction
Gamma correction is required to correct for the properties of digital display terminals. It also serves to better support human vision and the way light and color are perceived. The gray.colors() function has a default gamma = 2.2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Iterate gamma correction for n=12 # Configure graphical device par(mai=c(0.3, 0, 0.3, 0), bg = "ivory") g.grid <- layout(matrix(c(1,2,3,4, 5), nrow = 5)) g.gamma <- c(0.5, 1, 2, 4, 8) # Plot creation for(i in 1:5){ g.ramp <- gray.colors(12, start = 0, end = 1, gamma = gam[i]) barplot(rep(2, 12), axes = FALSE, space = 0, col = g.ramp, main = paste0("gamma = ", g.gamma[i])) } # Reset graphical device to default values par(op) |