# ----Packages------------------------------------------------------------- library("colorspace") library("ggplot2") theme_set(theme_minimal()) # ----Color spaces--------------------------------------------------------- par(mar = rep(0, 4)) d <- data.frame( x = c(1, 1, 2, 2, 3, 4, 5, 5, 5, 6), y = c(1, 3, 1, 3, 2, 2, 1, 3, 2, 2), lab = c("polarLAB", "polarLUV\n(= HCL)", "LAB", "LUV", "XYZ", "RGB", "HLS", "HSV", "sRGB", "hex"), stringsAsFactors = FALSE ) plot(0, 0, type = "n", axes = FALSE, xlab = "", ylab = "", xlim = c(0.7, 6.3), ylim = c(0.4, 3.6)) lines(y ~ x, data = d[-c(1, 3, 7, 8),]) lines(y ~ x, data = d[c(1, 3, 5),]) lines(y ~ x, data = d[c(6, 7, 9),]) lines(y ~ x, data = d[c(6, 8, 9),]) points(y ~ x, data = d[c(1, 2, 7, 8), ], pch = 21, bg = "lightgray", cex = 12) points(y ~ x, data = d[c(3, 4, 5, 6, 9), ], pch = 22, bg = "lightgray", cex = 12) points(y ~ x, data = d[10, ], pch = 23, bg = "white", cex = 12) text(d$x, d$y, d$lab) text(3.5, 2, "white point\n= D65", cex = 0.7, pos = 3) text(4.56, 2, "gamma\n(= 2.4)", cex = 0.7, pos = 3) # ---HCL vs. RGB----------------------------------------------------------- swatchplot( "Hue\n(Type of color)" = sequential_hcl(5, h = c(0, 300), c = c(60, 60), l = 65), "Chroma\n(Colorfulness)" = sequential_hcl(5, h = 0, c = c(100, 0), l = 65, rev = TRUE, power = 1), "Luminance\n(Brightness)" = sequential_hcl(5, h = 260, c = c(25, 25), l = c(25, 90), rev = TRUE, power = 1), nrow = 3, line = 5.2, off = 0 ) swatchplot( "Red" = rgb(0:4/4, 0, 0), "Green" = rgb(0, 0:4/4, 0), "Blue" = rgb(0, 0, 0:4/4), nrow = 3, off = 0 ) specplot(rainbow_hcl(99, c = 60, l = 75), rgb = TRUE) specplot(rainbow(95), rgb = TRUE) # ---HCL palettes---------------------------------------------------------- swatchplot( "Qualitative (Set 2)" = rbind("Color" = qualitative_hcl(5, "Set 2"), "Desaturated" = desaturate(qualitative_hcl(5, "Set 2"))), "Sequential (Blues 3)" = rbind("Color" = sequential_hcl(7, "Blues 3"), "Desaturated" = desaturate(sequential_hcl(7, "Blues 3"))), "Diverging (Green-Brown)" = rbind("Color" = diverging_hcl(7, "Green-Brown"), "Desaturated" = desaturate(diverging_hcl(7, "Green-Brown"))), nrow = 3, line = 7) swatchplot( "Blues 2" = sequential_hcl(7, "Blues 2"), "Blues 3" = sequential_hcl(7, "Blues 3"), "Blues" = sequential_hcl(7, "Blues") ) specplot(sequential_hcl(7, "Blues 2")) specplot(sequential_hcl(7, "Blues 3")) specplot(sequential_hcl(7, "Blues")) specplot(diverging_hcl(9, "Blue-Red", h2 = 5, rev = TRUE)) specplot(diverging_hcl(9, "Green-Brown")) hcl_palettes(plot = TRUE) # ----Base graphics-------------------------------------------------------- q4 <- qualitative_hcl(4, palette = "Dark 3") plot(log(EuStockMarkets), plot.type = "single", col = q4, lwd = 2) legend("topleft", colnames(EuStockMarkets), col = q4, lwd = 3, bty = "n") ttnc <- margin.table(Titanic, c(1, 4))[, 2:1] spineplot(ttnc, col = sequential_hcl(2, palette = "Purples 3")) # ----ggplot2 graphics----------------------------------------------------- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_density(alpha = 0.6) + scale_fill_discrete_qualitative(palette = "Dark 3") dsamp <- diamonds[1 + 1:1000 * 50, ] ggplot(dsamp, aes(carat, price, color = cut)) + geom_point() + scale_color_discrete_sequential(palette = "Purples 3", nmax = 6, order = 2:6) hclplot(qualitative_hcl(7, palette = "Set 2")) hclplot( sequential_hcl(7, palette = "Blues 3")) hclplot( diverging_hcl(7, palette = "Blue-Red")) par(mfrow = c(2, 5)) cl <- sequential_hcl(5, "Heat") for (i in c("map", "heatmap", "scatter", "spine", "bar", "pie", "perspective", "mosaic", "lines")) { demoplot(cl, type = i) mtext(side = 3, text = i, line = 1.5) } # ----Color vision deficiencies-------------------------------------------- z <- 2 * volcano x <- 10 * (1:nrow(z)) y <- 10 * (1:ncol(z)) par(mar = rep(0, 4)) persp(x, y, z, theta = 10, phi = 30, col = "lightgray", scale = FALSE, ltheta = 100, shade = 0.5, border = NA, box = FALSE) par(mfrow = c(2, 3), mar = rep(0.5, 4), oma = c(1, 20, 3, 1), las = 1) rain <- rainbow(11, end = 2/3) blyl <- sequential_hcl(11, "Blue-Yellow") demoplot(rain, "heatmap") mtext("rainbow(11, end = 2/3)", side = 2, line = 1) mtext("Original", line = 1) demoplot(desaturate(rain), "heatmap") mtext("Desaturated", line = 1) demoplot(deutan(rain), "heatmap") mtext("Deuteranope", line = 1) demoplot(blyl, "heatmap") mtext("sequential_hcl(11, \"Blue-Yellow\")", side = 2, line = 1) demoplot(desaturate(blyl), "heatmap") demoplot(deutan(blyl), "heatmap") # ----Approximating other palettes----------------------------------------- specplot( rev(RColorBrewer::brewer.pal(9, "YlGnBu")), sequential_hcl(9, "YlGnBu"), main = "ColorBrewer.org: YlGnBu" ) specplot( viridis::viridis(9), sequential_hcl(9, "Viridis"), main = "Viridis") # ----grDevices::hcl.colors()---------------------------------------------- hcl.swatch <- function(palette = hcl.pals(), n = 5, nrow = 11, border = if (n < 15) "black" else NA) { cols <- sapply(palette, hcl.colors, n = n) ncol <- ncol(cols) nswatch <- min(ncol, nrow) par(mar = rep(0.1, 4), mfrow = c(1, min(10, ncol %/% nrow + 1)), pin = c(1, 0.5 * nswatch), cex = 0.7) while (length(palette)) { subset <- 1:min(nrow, ncol(cols)) plot.new() plot.window(c(0, n), c(0, nrow + 1)) text(0, rev(subset) + 0.1, palette[subset], adj = c(0, 0)) y <- rep(subset, each = n) rect(rep(0:(n-1), n), rev(y), rep(1:n, n), rev(y) - 0.5, col = cols[, subset], border = border) palette <- palette[-subset] cols <- cols[, -subset] } par(mfrow = c(1, 1), mar = c(5.1, 4.1, 4.1, 2.1), cex = 1) } hcl.swatch()