| 1 |
#' Color Palette Demonstration Plot |
| 2 |
#' |
| 3 |
#' Demonstration of color palettes in various kinds of statistical graphics. |
| 4 |
#' |
| 5 |
#' To demonstrate how different kinds of color palettes work in different |
| 6 |
#' kinds of statistical displays, \code{demoplot} provides a simple convenience |
| 7 |
#' interface to some base graphics with (mostly artificial) data sets. |
| 8 |
#' All types of demos can deal with arbitrarily many colors. However, some |
| 9 |
#' displays are much more suitable for a low number of colors (e.g., the pie |
| 10 |
#' chart) while others work better with more colors (e.g., the heatmap). |
| 11 |
#' |
| 12 |
#' @param x character vector containing color hex codes. |
| 13 |
#' @param type character indicating the type of demonstration plot. |
| 14 |
#' @param \dots currently not used. |
| 15 |
#' @return \code{demoplot} returns invisibly what the respective base graphics |
| 16 |
#' functions return that are called internally. |
| 17 |
#' @seealso \code{\link{specplot}}, \code{\link{hclplot}} |
| 18 |
#' @references Zeileis A, Fisher JC, Hornik K, Ihaka R, McWhite CD, Murrell P, Stauffer R, Wilke CO (2020). |
| 19 |
#' \dQuote{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.} |
| 20 |
#' \emph{Journal of Statistical Software}, \bold{96}(1), 1--49. \doi{10.18637/jss.v096.i01} |
| 21 |
#' @keywords hplot |
| 22 |
#' @examples |
| 23 |
#' ## all built-in demos with the same sequential heat color palette |
| 24 |
#' par(mfrow = c(3, 3)) |
| 25 |
#' cl <- sequential_hcl(5, "Heat") |
| 26 |
#' for (i in c("map", "heatmap", "scatter", "spine", "bar", "pie", "perspective", "mosaic", "lines")) { |
| 27 |
#' demoplot(cl, type = i) |
| 28 |
#' } |
| 29 |
#' |
| 30 |
#' ## qualitative palettes: light pastel colors for shading areas (pie) |
| 31 |
#' ## and darker colorful palettes for points or lines |
| 32 |
#' demoplot(qualitative_hcl(4, "Pastel 1"), type = "pie") |
| 33 |
#' demoplot(qualitative_hcl(4, "Set 2"), type = "scatter") |
| 34 |
#' demoplot(qualitative_hcl(4, "Dark 3"), type = "lines") |
| 35 |
#' |
| 36 |
#' ## sequential palettes: display almost continuous gradients with |
| 37 |
#' ## strong luminance contrasts (heatmap, perspective) and colorful |
| 38 |
#' ## sequential palette for spine plot with only a few ordered categories |
| 39 |
#' demoplot(sequential_hcl(99, "Purple-Blue"), type = "heatmap") |
| 40 |
#' demoplot(sequential_hcl(99, "Reds"), type = "perspective") |
| 41 |
#' demoplot(sequential_hcl(4, "Viridis"), type = "spine") |
| 42 |
#' |
| 43 |
#' ## diverging palettes: display almost continuous gradient with |
| 44 |
#' ## strong luminance contrast bringing out the extremes (map), |
| 45 |
#' ## more colorful palette with lower luminance contrasts for displays |
| 46 |
#' ## with fewer colors (mosaic, bar) |
| 47 |
#' demoplot(diverging_hcl(99, "Tropic", power = 2.5), type = "map") |
| 48 |
#' demoplot(diverging_hcl(5, "Green-Orange"), type = "mosaic") |
| 49 |
#' demoplot(diverging_hcl(5, "Blue-Red 2"), type = "bar") |
| 50 |
#' |
| 51 |
#' ## some palettes that work well on black backgrounds |
| 52 |
#' par(mfrow = c(2, 3), bg = "black") |
| 53 |
#' demoplot(sequential_hcl(9, "Oslo"), "heatmap") |
| 54 |
#' demoplot(sequential_hcl(9, "Turku"), "heatmap") |
| 55 |
#' demoplot(sequential_hcl(9, "Inferno", rev = TRUE), "heatmap") |
| 56 |
#' demoplot(qualitative_hcl(9, "Set 2"), "lines") |
| 57 |
#' demoplot(diverging_hcl(9, "Berlin"), "scatter") |
| 58 |
#' demoplot(diverging_hcl(9, "Cyan-Magenta", l2 = 20), "lines") |
| 59 |
#' |
| 60 |
#' @export demoplot |
| 61 |
#' @importFrom graphics barplot image persp pie plot polygon rect segments |
| 62 |
demoplot <- function(x, |
| 63 |
type = c("map", "heatmap", "scatter", "spine", "bar", "pie", "perspective", "mosaic", "lines"), |
| 64 |
...) |
| 65 |
{ |
| 66 |
type <- match.arg(type, |
| 67 |
c("map", "heatmap", "scatter", "spine", "bar", "pie", "perspective", "mosaic", "lines")) |
| 68 |
do.call(paste("plot", type, sep = "_"), list(x = x, ...)) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
# Plot map example |
| 73 |
plot_map <- function(x, ...) { |
| 74 |
n <- length(x) |
| 75 |
plot(0, 0, type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n", bty = "n", |
| 76 |
xlim = c(-88.5, -78.6), ylim = c(30.2, 35.2), asp = 1) |
| 77 |
polygon(colorspace::USSouthPolygon, |
| 78 |
col = x[cut(stats::na.omit(colorspace::USSouthPolygon$z), |
| 79 |
breaks = 0:n / n)]) |
| 80 |
} |
| 81 |
|
| 82 |
# Plot heatmap example |
| 83 |
plot_heatmap <- function(x, ...) { |
| 84 |
image(datasets::volcano, col = rev(x), bty = "n", xaxt = "n", yaxt = "n", useRaster = TRUE) |
| 85 |
} |
| 86 |
|
| 87 |
# Plot scatter example |
| 88 |
.example_env <- new.env() |
| 89 |
.example_env$xyhclust <- NULL |
| 90 |
plot_scatter <- function(x, ...) { |
| 91 |
|
| 92 |
# Generate artificial data |
| 93 |
if (is.null(.example_env$xyhclust)) { |
| 94 |
set.seed(1071) |
| 95 |
x0 <- sin(pi * 1:60 / 30) / 5 |
| 96 |
y0 <- cos(pi * 1:60 / 30) / 5 |
| 97 |
xr <- c(0.1, -0.6, -0.7, -0.9, 0.4, 1.3, 1.0) |
| 98 |
yr <- c(0.3, 1.0, 0.1, -0.9, -0.8, -0.4, 0.6) |
| 99 |
dat <- data.frame( |
| 100 |
x=c(x0 + xr[1], x0 + xr[2], x0 + xr[3], x0 + xr[4], x0 + xr[5], |
| 101 |
x0 + xr[6], x0 + xr[7]), |
| 102 |
y=c(y0 + yr[1], y0 + yr[2], y0 + yr[3], y0 + yr[4], y0 + yr[5], |
| 103 |
y0 + yr[6], y0 + yr[7]) |
| 104 |
) |
| 105 |
attr(dat, "hclust") <- stats::hclust(stats::dist(dat), method = "ward.D") |
| 106 |
dat$xerror <- stats::rnorm(nrow(dat), sd=stats::runif(nrow(dat), 0.05, 0.45)) |
| 107 |
dat$yerror <- stats::rnorm(nrow(dat), sd=stats::runif(nrow(dat), 0.05, 0.45)) |
| 108 |
.example_env$xyhclust <- dat |
| 109 |
} |
| 110 |
plot(.example_env$xyhclust$x + |
| 111 |
.example_env$xyhclust$xerror, |
| 112 |
.example_env$xyhclust$y + |
| 113 |
.example_env$xyhclust$yerror, |
| 114 |
col = "black", bg = x[stats::cutree(attr(.example_env$xyhclust, "hclust"), length(x))], |
| 115 |
xlab = "", ylab = "", axes = FALSE, pch = 21, cex = 1.3) |
| 116 |
} |
| 117 |
|
| 118 |
# Plot spine example |
| 119 |
plot_spine <- function(x, ...) { |
| 120 |
n <- length(x) |
| 121 |
|
| 122 |
# Rectangle dimensions |
| 123 |
off <- 0.015 |
| 124 |
widths <- c(0.05, 0.1, 0.15, 0.1, 0.2, 0.08, 0.12, 0.16, 0.04) |
| 125 |
k <- length(widths) |
| 126 |
heights <- sapply( |
| 127 |
c(2.5, 1.2, 2.7, 1, 1.3, 0.7, 0.4, 0.2, 1.7), |
| 128 |
function(p) (0:n / n)^(1 / p) |
| 129 |
) |
| 130 |
|
| 131 |
# Rectangle coordinates |
| 132 |
xleft0 <- c(0, cumsum(widths + off)[-k]) |
| 133 |
xleft <- rep(xleft0, each=n) |
| 134 |
xright <- xleft + rep(widths, each=n) |
| 135 |
ybottom <- as.vector(heights[-(n + 1), ]) |
| 136 |
ytop <- as.vector(heights[-1, ]) |
| 137 |
|
| 138 |
# Draw rectangles, borders, and annotation |
| 139 |
plot(0, 0, xlim=c(0, sum(widths) + off * (k - 1)), ylim=c(0, 1), |
| 140 |
xaxs="i", yaxs="i", main="", xlab="", ylab="", |
| 141 |
type="n", axes=FALSE) |
| 142 |
rect(xleft, ybottom, xright, ytop, col = rep(x, k), |
| 143 |
border = if(n < 10) "black" else "transparent") |
| 144 |
if(n >= 10) rect(xleft0, 0, xleft0 + widths, 1, border="black") |
| 145 |
} |
| 146 |
|
| 147 |
# Plot bar example |
| 148 |
plot_bar <- function(x, ...) { |
| 149 |
barplot(cbind(1.1 + abs(sin(0.5 + seq_along(x))) / 3, |
| 150 |
1.9 + abs(cos(1.1 + seq_along(x))) / 3, |
| 151 |
0.7 + abs(sin(1.5 + seq_along(x))) / 3, |
| 152 |
0.3 + abs(cos(0.8 + seq_along(x))) / 3), |
| 153 |
beside = TRUE, col = x, axes = FALSE) |
| 154 |
} |
| 155 |
|
| 156 |
# Plot pie example |
| 157 |
plot_pie <- function(x, ...) { |
| 158 |
pie(0.01 + abs(sin(0.5 + seq_along(x))), labels = "", col = x, radius = 1) |
| 159 |
} |
| 160 |
|
| 161 |
# Plot perspective example |
| 162 |
plot_perspective <- function(x, ...) { |
| 163 |
# Mixture of bivariate normals |
| 164 |
n <- 31 |
| 165 |
x1 <- x2 <- seq(-3, 3, length.out = n) |
| 166 |
y <- outer(x1, x2, |
| 167 |
function(x, y) { |
| 168 |
0.5 * stats::dnorm(x, mean = -1, sd = 0.80) * stats::dnorm(y, mean = -1, sd = 0.80) + |
| 169 |
0.5 * stats::dnorm(x, mean = 1, sd = 0.72) * stats::dnorm(y, mean = 1, sd = 0.72) |
| 170 |
} |
| 171 |
) |
| 172 |
|
| 173 |
# Compute color based on density |
| 174 |
if (length(x) > 1) { |
| 175 |
facet <- cut(y[-1, -1] + y[-1, -n] + y[-n, -1] + y[-n, -n], |
| 176 |
length(x)) |
| 177 |
cols <- rev(x)[facet] |
| 178 |
} else { |
| 179 |
cols <- x |
| 180 |
} |
| 181 |
|
| 182 |
# Perspective plot coding z-axis with color |
| 183 |
persp(x1, x2, y, col = cols, phi = 28, theta = 20, r = 5, xlab = "", ylab = "", zlab = "") |
| 184 |
} |
| 185 |
|
| 186 |
# Plot mosaic example |
| 187 |
.example_env$msc.matrix <- NULL |
| 188 |
plot_mosaic <- function(x, ...) { |
| 189 |
if (is.null(.example_env$msc.matrix)) { |
| 190 |
set.seed(1071) |
| 191 |
mat <- list() |
| 192 |
for (i in 1:50) { |
| 193 |
mat[[i]] <- matrix(stats::runif(i * 10, min = -1, max = 1), nrow = 10, ncol = i) |
| 194 |
} |
| 195 |
.example_env$msc.matrix <- mat |
| 196 |
} |
| 197 |
image(.example_env$msc.matrix[[length(x)]], bty = "n", col = x, xaxt = "n", yaxt = "n") |
| 198 |
} |
| 199 |
|
| 200 |
# Plot lines example |
| 201 |
plot_lines <- function(x, ...) { |
| 202 |
n <- length(x) |
| 203 |
plot(NULL, xlab = "", ylab = "", xaxt = "n", yaxt = "n", type = "n", |
| 204 |
bty = "n", xlim = c(0, 6), ylim = c(1.5, n + 1.5)) |
| 205 |
s <- 2:(n + 1) |
| 206 |
rev.s <- rev(s) |
| 207 |
rev.x <- rev(x) |
| 208 |
lwd <- 6 |
| 209 |
if (n > 5) |
| 210 |
lwd <- lwd -1 |
| 211 |
if (n > 15) |
| 212 |
lwd <- lwd -1 |
| 213 |
if (n > 25) |
| 214 |
lwd <- lwd -1 |
| 215 |
segments(1 / s, s, 2 + 1 / rev.s, rev.s, x, lwd = lwd) |
| 216 |
segments(2 + 1 / s, s, 4 - 1 / s, s, rev.x, lwd = lwd) |
| 217 |
segments(4 - 1 / s, s, 6 - 1 / s, rev.s, rev.x, lwd = lwd) |
| 218 |
} |
| 219 |
|
| 220 |
# Wrapper around specplot. Used by the tcltk interface. |
| 221 |
plot_spectrum <- function(x, cex = 1.0, plot = TRUE, rgb = TRUE, ...) |
| 222 |
specplot(x, cex = cex, plot = plot, rgb = rgb, ...) |
| 223 |
|
| 224 |
# Wrapper around hclplot. Used by the tcltk interface. |
| 225 |
plot_hclplot <- function(x, cex = 1.0, ...) |
| 226 |
hclplot(x, cex = cex, ...) |
| 227 |
|
| 228 |
|