| 1 |
\documentclass[nojss]{jss} |
| 2 |
|
| 3 |
%% need no \usepackage{Sweave} |
| 4 |
\SweaveOpts{engine=R, eps=FALSE, echo=FALSE, results=hide, keep.source = TRUE, pdf = TRUE, png = FALSE, resolution = 100} |
| 5 |
|
| 6 |
\title{HCL-Based Color Palettes in \proglang{R}} |
| 7 |
\Plaintitle{HCL-Based Color Palettes in R} |
| 8 |
\author{Achim Zeileis\\Universit\"at Innsbruck |
| 9 |
\And Kurt Hornik\\WU Wirtschafts-\\universit\"at Wien |
| 10 |
\And Paul Murrell\\The University\\of Auckland} |
| 11 |
\Plainauthor{Achim Zeileis, Kurt Hornik, Paul Murrell} |
| 12 |
\Keywords{HCL colors, qualitative palette, sequential palette, diverging palette} |
| 13 |
|
| 14 |
\Abstract{ |
| 15 |
This introduction to HCL-based color palettes in \proglang{R} package \pkg{colorspace} |
| 16 |
was written to accompany \cite{color:Zeileis+Hornik+Murrell:2009}. Since then the |
| 17 |
package's capabilities have been substantially extended and an up-to-date overview |
| 18 |
is available at \url{https://colorspace.R-Forge.R-project.org/} which is recommended |
| 19 |
as the main reference. For historical completeness this PDF vignette is still |
| 20 |
preserved, though. |
| 21 |
|
| 22 |
The package \pkg{colorspace} provides various functions providing perceptually-based |
| 23 |
color palettes for coding categorical data (qualitative palettes) and numerical |
| 24 |
variables (sequential and diverging palettes). We illustrate how these |
| 25 |
functions can be employed to generate various flavours of these palettes (with |
| 26 |
different emphases) and how they can be plugged into \proglang{R} graphics |
| 27 |
functions. |
| 28 |
} |
| 29 |
|
| 30 |
\Address{ |
| 31 |
Achim Zeileis\\ |
| 32 |
Department of Statistics\\ |
| 33 |
Faculty of Economics and Statistics\\ |
| 34 |
Universit\"at Innsbruck\\ |
| 35 |
Universit\"atsstra{\ss}e~15\\ |
| 36 |
6020 Innsbruck, Austria\\ |
| 37 |
E-mail: \email{[email protected]}\\ |
| 38 |
URL: \url{https://www.zeileis.org/}\\ |
| 39 |
|
| 40 |
Kurt Hornik\\ |
| 41 |
Institute for Statistics and Mathematics\\ |
| 42 |
Department of Finance, Accounting and Statistics\\ |
| 43 |
WU Wirtschaftsuniversit\"at Wien\\ |
| 44 |
Welthandelsplatz 1\\ |
| 45 |
1020 Wien, Austria\\ |
| 46 |
E-mail: \email{[email protected]}\\ |
| 47 |
URL: \url{https://statmath.wu.ac.at/~hornik/}\\ |
| 48 |
|
| 49 |
Paul Murrell\\ |
| 50 |
Department of Statistics\\ |
| 51 |
The University of Auckland\\ |
| 52 |
Private Bag 92019\\ |
| 53 |
Auckland, New Zealand\\ |
| 54 |
E-mail: \email{[email protected]}\\ |
| 55 |
URL: \url{https://www.stat.auckland.ac.nz/~paul/} |
| 56 |
} |
| 57 |
|
| 58 |
\begin{document} |
| 59 |
|
| 60 |
%\VignetteIndexEntry{HCL-Based Color Palettes in R} |
| 61 |
%\VignetteDepends{colorspace,vcd,MASS,KernSmooth,mvtnorm,kernlab} |
| 62 |
%\VignetteKeywords{HCL colors, qualitative palette, sequential palette, diverging palette} |
| 63 |
%\VignettePackage{colorspace} |
| 64 |
|
| 65 |
<<preliminaries>>= |
| 66 |
options(prompt = "R> ", continue = "+ ") |
| 67 |
library("colorspace") |
| 68 |
library("vcd") |
| 69 |
library("mvtnorm") |
| 70 |
library("kernlab") |
| 71 |
library("KernSmooth") |
| 72 |
@ |
| 73 |
|
| 74 |
\section{Introduction} \label{sec:intro} |
| 75 |
|
| 76 |
This is a companion vignette to \cite{color:Zeileis+Hornik+Murrell:2009} |
| 77 |
providing further technical details on the construction of the palettes as well as |
| 78 |
\proglang{R} code illustrating the use of varying palettes in practice. |
| 79 |
The palettes as well as some graphical functions demonstrated are contained |
| 80 |
in the package \pkg{colorspace}.\footnote{In addition to the discussion here, |
| 81 |
a graphical user interface for choosing colors based on the ideas of |
| 82 |
\cite{color:Zeileis+Hornik+Murrell:2009} is provided in the package as |
| 83 |
\code{choose\_palette()}.} |
| 84 |
|
| 85 |
As a simple convenience function we will use the function |
| 86 |
<<pal, echo=TRUE>>= |
| 87 |
pal <- function(col, border = "light gray", ...) |
| 88 |
{ |
| 89 |
n <- length(col) |
| 90 |
plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1), |
| 91 |
axes = FALSE, xlab = "", ylab = "", ...) |
| 92 |
rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border) |
| 93 |
} |
| 94 |
@ |
| 95 |
which displays a set of colors using a sequence of shaded rectangles. |
| 96 |
|
| 97 |
In the remainder of this vignette, we first outline how different types of palettes |
| 98 |
can be constructed and generated using the tools from \pkg{colorspace}. Subsequently, |
| 99 |
we present a collection of examples illustrating the tools in practice. |
| 100 |
|
| 101 |
\pagebreak |
| 102 |
|
| 103 |
\section{Color palettes} \label{sec:palettes} |
| 104 |
|
| 105 |
\subsection{Qualitative palettes} \label{sec:qualitative} |
| 106 |
|
| 107 |
Qualitative palettes are sets of colors for depicting different categories, |
| 108 |
i.e., for coding a categorical variable. To give the same |
| 109 |
perceptual weight to each category, chroma and luminance are kept constant |
| 110 |
and only the hue is varied for obtaining different colors |
| 111 |
(which are consequently all balanced towards the same gray). |
| 112 |
|
| 113 |
In \pkg{colorspace}, qualitative palettes are implemented in the function |
| 114 |
\begin{Soutput} |
| 115 |
rainbow_hcl(n, c = 50, l = 70, start = 0, end = 360*(n-1)/n, ...) |
| 116 |
\end{Soutput} |
| 117 |
where \code{n} controls the number of colors in the palette. The arguments |
| 118 |
\code{c} and \code{l} give the fixed chroma and luminance levels, respectively, |
| 119 |
and \code{start} and \code{end} specify the range of hue angles. The function |
| 120 |
is named after the base \proglang{R} function \code{rainbow()} which has a similar |
| 121 |
interface but chooses colors in HSV coordinates. |
| 122 |
Figure~\ref{fig:pal-q-examples} depicts |
| 123 |
examples for generating qualitative sets of colors $(H, 50, 70)$ produced by |
| 124 |
<<pal-q, echo=TRUE, eval=FALSE>>= |
| 125 |
pal(rainbow_hcl(4, start = 30, end = 300), main = "dynamic") |
| 126 |
pal(rainbow_hcl(4, start = 60, end = 240), main = "harmonic") |
| 127 |
pal(rainbow_hcl(4, start = 270, end = 150), main = "cold") |
| 128 |
pal(rainbow_hcl(4, start = 90, end = -30), main = "warm") |
| 129 |
@ |
| 130 |
From left to right and top to down, this shows a palette from the full spectrum ($H = 30, 120, 210, 300$) |
| 131 |
creating a `dynamic' set of colors, a `harmonic' |
| 132 |
set with $H = 60, 120, 180, 240$, cold colors (from the blue/green part of the spectrum: |
| 133 |
$H = 270, 230, 190, 150$) and warm colors (from the yellow/red part of the spectrum: |
| 134 |
$H = 90, 50, 10, 330$), respectively. |
| 135 |
|
| 136 |
\setkeys{Gin}{width=0.75\textwidth} |
| 137 |
\begin{figure}[p!] |
| 138 |
\begin{center} |
| 139 |
<<pal-q1, fig=TRUE, height=4, width=6>>= |
| 140 |
par(mfrow = c(2, 2), mar = c(0, 0, 3, 0)) |
| 141 |
<<pal-q>> |
| 142 |
@ |
| 143 |
\caption{\label{fig:pal-q-examples} Examples for qualitative palettes. Hue is varied |
| 144 |
in different intervals for given $C = 50$ and $L = 70$.} |
| 145 |
\end{center} |
| 146 |
\end{figure} |
| 147 |
|
| 148 |
|
| 149 |
\subsection{Sequential palettes} \label{sec:sequential} |
| 150 |
|
| 151 |
Sequential palettes are used for coding numerical information that ranges |
| 152 |
in a certain interval where low values are considered to be uninteresting and |
| 153 |
high values are interesting. Suppose we need to visualize an intensity or |
| 154 |
interestingness $i$ which (without loss of generality) is scaled to the unit |
| 155 |
interval. |
| 156 |
|
| 157 |
Potentially, all three dimensions of HCL space can be used for coding the intensity, |
| 158 |
leading to colors from an interval of hues (i.e., differing types of colors), |
| 159 |
an interval of chroma values (i.e., differing colorfulness) and an interval of |
| 160 |
luminance (i.e., differing intensity of gray). If we allow chroma and luminance |
| 161 |
to increase non-linearly via a function of type $i^p$, the resulting formula is: |
| 162 |
\[ (H_2 - i \cdot (H_1 - H_2), C_{\max} - i^{p_1} \cdot (C_{\max} - C_{\min}), |
| 163 |
L_{\max} - i^{p_2} \cdot (L_{\max} - L_{\min})). \] |
| 164 |
|
| 165 |
Two different ``flavors'' of this formula are readily implemented in \pkg{colorspace}, |
| 166 |
employing different defaults. For single hue palettes with $H_1 = H_2$ the |
| 167 |
function |
| 168 |
\begin{Soutput} |
| 169 |
sequential_hcl(n, h = 260, c = c(80, 0), l = c(30, 90), power = 1.5, ...) |
| 170 |
\end{Soutput} |
| 171 |
is provided where the first element of \code{c} and \code{l} give the starting chroma and luminance |
| 172 |
coordinate (by default colorful and dark) and the second element the ending coordinate |
| 173 |
(by default gray and light). The \code{power} argument implements the parameter $p$ |
| 174 |
from the $i^p$ function (and can be a vector of length 2). Sequential palettes using an |
| 175 |
interval of hues are provided by |
| 176 |
\begin{Soutput} |
| 177 |
heat_hcl(n, h = c(0, 90), c = c(100, 30), l = c(50, 90), power = c(1/5, 1), ...) |
| 178 |
\end{Soutput} |
| 179 |
named after the HSV-based \proglang{R} function \code{heat.colors()} and |
| 180 |
by default starts from a red and going to a yellow hue. The defaults in \code{heat_hcl()} |
| 181 |
are set differently compared to \code{sequential_hcl()} as to make the default HCL heat |
| 182 |
colors more similar to the HSV version. The defaults of \code{sequential_hcl()}, on the |
| 183 |
other hand, are set as to achieve a large contrast on the luminance axis. In addition |
| 184 |
\code{terrain_hcl()} is a wrapper for \code{heat_hcl()} producing colors similar to |
| 185 |
the HSV-based \code{terrain.colors()} from base \proglang{R}. |
| 186 |
|
| 187 |
Various palettes produced from these functions are shown in |
| 188 |
Figure~\ref{fig:pal-s-examples} using different pairs of hues as well as different |
| 189 |
chroma and luminance contrasts. |
| 190 |
<<pal-s, echo=TRUE, eval=FALSE>>= |
| 191 |
pal(sequential_hcl(12, c = 0, power = 2.2)) |
| 192 |
pal(sequential_hcl(12, power = 2.2)) |
| 193 |
pal(heat_hcl(12, c = c(80, 30), l = c(30, 90), power = c(1/5, 2))) |
| 194 |
pal(terrain_hcl(12, c = c(65, 0), l = c(45, 90), power = c(1/2, 1.5))) |
| 195 |
pal(rev(heat_hcl(12, h = c(0, -100), c = c(40, 80), l = c(75, 40), |
| 196 |
power = 1))) |
| 197 |
@ |
| 198 |
|
| 199 |
\setkeys{Gin}{width=.75\textwidth} |
| 200 |
\begin{figure}[p!] |
| 201 |
\begin{center} |
| 202 |
<<pal-s1, fig=TRUE, height=5, width=6>>= |
| 203 |
par(mfrow = c(5, 1), mar = c(0, 0, 0, 0)) |
| 204 |
<<pal-s>> |
| 205 |
@ |
| 206 |
\caption{\label{fig:pal-s-examples} Examples for sequential palettes, varying |
| 207 |
only luminance (first panel), chroma and luminance (second panel), and hue, |
| 208 |
chroma and luminance (remaining panels).} |
| 209 |
\end{center} |
| 210 |
\end{figure} |
| 211 |
|
| 212 |
|
| 213 |
\subsection{Diverging palettes} \label{sec:diverging} |
| 214 |
|
| 215 |
Diverging palettes are also used for coding numerical information ranging in a certain |
| 216 |
interval---however, this interval includes a neutral value. Analogously to the previous |
| 217 |
section, we suppose that we want to visualize an intensity or interestingness $i$ |
| 218 |
from the interval $[-1, 1]$ (without loss of generality). |
| 219 |
Given useful sequential palettes, deriving diverging palettes is easy: two different hues are |
| 220 |
chosen for adding color to the same amount of `gray' at a given intensity $|i|$. |
| 221 |
|
| 222 |
Diverging palettes are implemented in the function |
| 223 |
\begin{Soutput} |
| 224 |
diverging_hcl(n, h = c(260, 0), c = 80, l = c(30, 90), power = 1.5, ...) |
| 225 |
\end{Soutput} |
| 226 |
which has the same arguments as \code{sequential_hcl()} but takes a pair of hues \code{h}. |
| 227 |
|
| 228 |
Figure~\ref{fig:pal-d-examples} shows various examples of conceivable combinations of |
| 229 |
hue, chroma and luminance. The first palette uses a broader range on the luminance axis |
| 230 |
whereas the others mostly rely on chroma contrasts. |
| 231 |
<<pal-d, echo=TRUE, eval=FALSE>>= |
| 232 |
pal(diverging_hcl(7)) |
| 233 |
pal(diverging_hcl(7, c = 100, l = c(50, 90), power = 1)) |
| 234 |
pal(diverging_hcl(7, h = c(130, 43), c = 100, l = c(70, 90))) |
| 235 |
pal(diverging_hcl(7, h = c(180, 330), c = 59, l = c(75, 95))) |
| 236 |
@ |
| 237 |
|
| 238 |
\setkeys{Gin}{width=.8\textwidth} |
| 239 |
\begin{figure}[t!] |
| 240 |
\begin{center} |
| 241 |
<<pal-d1, fig=TRUE, height=4, width=6>>= |
| 242 |
par(mfrow = c(4, 1), mar = c(0, 0, 0, 0)) |
| 243 |
<<pal-d>> |
| 244 |
@ |
| 245 |
\caption{\label{fig:pal-d-examples} Examples for diverging palettes with different |
| 246 |
pairs of hues and decreasing luminance contrasts.} |
| 247 |
\end{center} |
| 248 |
\end{figure} |
| 249 |
|
| 250 |
\pagebreak |
| 251 |
|
| 252 |
\section{Illustrations} \label{sec:illustrations} |
| 253 |
|
| 254 |
\subsection{Qualitative palettes: Seats and votes in the German Bundestag} |
| 255 |
|
| 256 |
In this section, we show a collection of examples for the various types of palettes applied |
| 257 |
to statistical graphics. The first example illustrates qualitative palettes |
| 258 |
and visualizes data from the 2005 election for the German parliament ``Bundestag''. |
| 259 |
In this election, five parties were able to obtain enough votes to enter the |
| 260 |
Bundestag, the numbers of seats are given by |
| 261 |
|
| 262 |
<<seats-data, echo=TRUE>>= |
| 263 |
seats <- setNames(c(226, 61, 54, 51, 222), |
| 264 |
c("CDU/CSU", "FDP", "Linke", "Gruene", "SPD")) |
| 265 |
seats |
| 266 |
@ |
| 267 |
|
| 268 |
We choose colors that are rough metaphors for the political parties, using a red hue $H = 0$ for the |
| 269 |
social democrats SPD, a blue hue $H = 240$ for the conservative CDU/CSU, a yellow hue $H = 60$ |
| 270 |
for the liberal FDP, a green hue $H = 120$ for the green party ``Die Gr\"unen'' and a purple |
| 271 |
hue $H = 300$ for the leftist party ``Die Linke''. To obtain rather intense colors, we set |
| 272 |
chroma to $C = 60$ and luminance to $L = 75$: |
| 273 |
|
| 274 |
<<seats-colors, echo=TRUE>>= |
| 275 |
parties <- rainbow_hcl(6, c = 60, l = 75)[c(5, 2, 6, 3, 1)] |
| 276 |
names(parties) <- names(seats) |
| 277 |
@ |
| 278 |
|
| 279 |
The distribution of seats is depicted in a pie chart in Figure~\ref{fig:seats}. |
| 280 |
showing clearly that neither the governing |
| 281 |
coalition of SPD and Gr\"une nor the opposition of CDU/CSU and FDP could assemble a majority. |
| 282 |
Given that no party would enter a coalition with the leftists, this lead to a big coalition |
| 283 |
of CDU/CSU and SPD. |
| 284 |
<<seats, echo=TRUE, eval=FALSE>>= |
| 285 |
pie(seats, clockwise = TRUE, col = parties, radius = 1) |
| 286 |
@ |
| 287 |
|
| 288 |
\setkeys{Gin}{width=.6\textwidth} |
| 289 |
\begin{figure}[p] |
| 290 |
\begin{center} |
| 291 |
<<seats1, fig=TRUE, height=5, width=6>>= |
| 292 |
par(mar = rep(0.8, 4)) |
| 293 |
<<seats>> |
| 294 |
@ |
| 295 |
\caption{\label{fig:seats} Seats in the German parliament.} |
| 296 |
\end{center} |
| 297 |
\end{figure} |
| 298 |
|
| 299 |
To take a closer look at the regional distribution, we load the \code{Bundestag2005} |
| 300 |
data set containing a contingency table with the number of votes for each party |
| 301 |
stratified by province (Bundesland). Then, we re-order the provinces from |
| 302 |
north to south, first the 10 western provinces (the former Federal Republic of Germany, FRG), |
| 303 |
then the 6 eastern provinces (the former German Democratic Republic, GDR). |
| 304 |
<<votes-data, echo=TRUE>>= |
| 305 |
data("Bundestag2005", package = "vcd") |
| 306 |
votes <- Bundestag2005[c(1, 3:5, 9, 11, 13:16, 2, 6:8, 10, 12), |
| 307 |
c("CDU/CSU", "FDP", "SPD", "Gruene", "Linke")] |
| 308 |
@ |
| 309 |
|
| 310 |
The data can then be visualized using a highlighted mosaic display via |
| 311 |
<<votes, echo=TRUE, eval=FALSE>>= |
| 312 |
mosaic(votes, gp = gpar(fill = parties[colnames(votes)])) |
| 313 |
@ |
| 314 |
The annotation for this plot is clearly sub-optimal, hence we use the |
| 315 |
flexible \code{strucplot()} framework provided by \pkg{vcd} and display |
| 316 |
the data via |
| 317 |
<<votes, echo=TRUE, eval=FALSE>>= |
| 318 |
mosaic(votes, gp = gpar(fill = parties[colnames(votes)]), |
| 319 |
spacing = spacing_highlighting, labeling = labeling_left, |
| 320 |
labeling_args = list(rot_labels = c(0, 90, 0, 0), |
| 321 |
varnames = FALSE, pos_labels = "center", |
| 322 |
just_labels = c("center", "center", "center", "right")), |
| 323 |
margins = unit(c(2.5, 1, 1, 12), "lines"), |
| 324 |
keep_aspect_ratio = FALSE) |
| 325 |
@ |
| 326 |
The output is shown in Figure~\ref{fig:votes} highlighting that |
| 327 |
the SPD performed better in the north and the CDU/CSU better in the south; furthermore, |
| 328 |
Die Linke performed particularly well in the eastern provinces and in Saarland. |
| 329 |
|
| 330 |
|
| 331 |
\setkeys{Gin}{width=.8\textwidth} |
| 332 |
\begin{figure}[p] |
| 333 |
\begin{center} |
| 334 |
<<votes1, fig=TRUE, height=7, width=8.6>>= |
| 335 |
<<votes>> |
| 336 |
@ |
| 337 |
\caption{\label{fig:votes} Votes in the German election 2005.} |
| 338 |
\end{center} |
| 339 |
\end{figure} |
| 340 |
|
| 341 |
\subsection{Sequential palettes: Old Faithful geyser eruptions} |
| 342 |
|
| 343 |
To illustrate sequential palettes, a bivariate density estimation for the Old |
| 344 |
Faithful geyser eruptions data \code{geyser} from \pkg{MASS} is visualized. |
| 345 |
The Old Faithful geyser is one of the most popular sites in Yellowstone National Park |
| 346 |
and it is of some interest to understand the relation ship between the duration |
| 347 |
of a geyser eruption and the waiting time for this eruption. To look at the data, |
| 348 |
we use a bivariate kernel density estimate provided |
| 349 |
by the function \code{bkde2D()} from package \pkg{KernSmooth}. |
| 350 |
<<bkde-fit, echo=TRUE>>= |
| 351 |
library("KernSmooth") |
| 352 |
data("geyser", package = "MASS") |
| 353 |
dens <- bkde2D(geyser[,2:1], bandwidth = c(0.2, 3), gridsize = c(201, 201)) |
| 354 |
@ |
| 355 |
|
| 356 |
Subsequently, we look at the estimated kernel density by means of a heatmap |
| 357 |
(produced via \code{image()}) using two different sequential palettes: first |
| 358 |
with only gray colors |
| 359 |
<<bkde1, echo=TRUE, eval=FALSE>>= |
| 360 |
image(dens$x1, dens$x2, dens$fhat, xlab = "duration", ylab = "waiting time", |
| 361 |
col = rev(heat_hcl(33, c = 0, l = c(30, 90), power = c(1/5, 1.3)))) |
| 362 |
@ |
| 363 |
and then using heat colors balanced towards the sam gray levels as above |
| 364 |
<<bkde2, echo=TRUE, eval=FALSE>>= |
| 365 |
image(dens$x1, dens$x2, dens$fhat, xlab = "duration", ylab = "waiting time", |
| 366 |
col = rev(heat_hcl(33, c = c(80, 30), l = c(30, 90), power = c(1/5, 1.3)))) |
| 367 |
@ |
| 368 |
Figure~\ref{fig:bkde} shows the resulting heatmaps revealing a multi-modal |
| 369 |
bivariate distribution: short waiting |
| 370 |
times (around 50 minutes) are typically followed by a long eruption (around 4 minutes) |
| 371 |
whereas long waiting times (around 80 minutes) can be followed by either a long |
| 372 |
or short eruption (around 4 minutes). |
| 373 |
|
| 374 |
\setkeys{Gin}{width=\textwidth} |
| 375 |
\begin{figure}[t!] |
| 376 |
\begin{center} |
| 377 |
<<bkde3, fig=TRUE, height=6, width=12, png=TRUE, pdf=FALSE>>= |
| 378 |
par(mfrow = c(1, 2)) |
| 379 |
<<bkde1>> |
| 380 |
box() |
| 381 |
<<bkde2>> |
| 382 |
box() |
| 383 |
@ |
| 384 |
\caption{\label{fig:bkde} Bivariate density estimation for duration and waiting time for an eruption.} |
| 385 |
\end{center} |
| 386 |
\end{figure} |
| 387 |
|
| 388 |
Another interesting question in this data set would be ask how long the waiting |
| 389 |
time for the next eruption is following a short or long eruption respectively. |
| 390 |
This can be visualized using another bivarate density estimate for the transformed |
| 391 |
data set matching the previous duration with the following waiting time: |
| 392 |
<<bkde-fit2, echo=TRUE>>= |
| 393 |
library("KernSmooth") |
| 394 |
geyser2 <- cbind(geyser$duration[-299], geyser$waiting[-1]) |
| 395 |
dens2 <- bkde2D(geyser2, bandwidth = c(0.2, 3), gridsize = c(201, 201)) |
| 396 |
@ |
| 397 |
|
| 398 |
Again, we look at this density using two heatmaps generated via |
| 399 |
<<bkde4, echo=TRUE, eval=FALSE>>= |
| 400 |
image(dens2$x1, dens2$x2, dens2$fhat, xlab = "duration", ylab = "waiting time", |
| 401 |
col = rev(heat_hcl(33, c = 0, l = c(30, 90), power = c(1/5, 1.3)))) |
| 402 |
@ |
| 403 |
and |
| 404 |
<<bkde5, echo=TRUE, eval=FALSE>>= |
| 405 |
image(dens2$x1, dens2$x2, dens2$fhat, xlab = "duration", ylab = "waiting time", |
| 406 |
col = rev(heat_hcl(33, c = c(80, 30), l = c(30, 90), power = c(1/5, 1.3)))) |
| 407 |
@ |
| 408 |
Figure~\ref{fig:bkde2} shows the result that illustrates that long and short |
| 409 |
waiting times follow long and short eruption durations, resepectively. |
| 410 |
|
| 411 |
\setkeys{Gin}{width=\textwidth} |
| 412 |
\begin{figure}[t!] |
| 413 |
\begin{center} |
| 414 |
<<bkde6, fig=TRUE, height=6, width=12, png=TRUE, pdf=FALSE>>= |
| 415 |
par(mfrow = c(1, 2)) |
| 416 |
<<bkde4>> |
| 417 |
box() |
| 418 |
<<bkde5>> |
| 419 |
box() |
| 420 |
@ |
| 421 |
\caption{\label{fig:bkde2} Bivariate density estimation for previous duration and following waiting time for |
| 422 |
an eruption.} |
| 423 |
\end{center} |
| 424 |
\end{figure} |
| 425 |
|
| 426 |
\subsection{Diverging palettes: Arthritis and SVM classification} |
| 427 |
|
| 428 |
Diverging palettes are particularly useful |
| 429 |
when visualizing residuals or correlations (with natural neutral value 0) or probabilities |
| 430 |
in 2-class supervised learning (with neutral value 0.5). Examples for both situations are |
| 431 |
provided here. First, we look at the outcome for the female patients from a double-blind clinical |
| 432 |
trial investigating a new treatment for rheumatoid arthritis. |
| 433 |
<<arthritis-data, echo=TRUE>>= |
| 434 |
art <- xtabs(~ Treatment + Improved, data = Arthritis, |
| 435 |
subset = Sex == "Female") |
| 436 |
@ |
| 437 |
For visualizing the data, we use a mosaic display with maximum shading |
| 438 |
\citep[as derived by][]{color:Zeileis+Meyer+Hornik:2007} via |
| 439 |
<<arthritis, echo=TRUE, eval=FALSE>>= |
| 440 |
set.seed(1071) |
| 441 |
mosaic(art, gp = shading_max, gp_args = list(n = 5000)) |
| 442 |
@ |
| 443 |
|
| 444 |
The mosaic rectangles in Figure~\ref{fig:arthritis} |
| 445 |
signal that the treatment lead to higher improvement compared to the placebo group; this effect |
| 446 |
is shown to be significant by the shading that codes the size of the Pearon residuals. |
| 447 |
Positive residuals, corresponding |
| 448 |
to more observations in the corresponding cell than expected under independence, are depicted |
| 449 |
in blue, negative residuals in red. Light colors signal significance at 10\% level, full colors |
| 450 |
significance at 1\% level. The palette implicitly used in this plot is |
| 451 |
\code{diverging_hcl(5, c = c(100, 0), l = c(50, 90), power = 1)}, it can be modified using |
| 452 |
the arguments of \code{shading_max()} which has an interface similar to \code{diverging_hcl()}. |
| 453 |
|
| 454 |
\setkeys{Gin}{width=.7\textwidth} |
| 455 |
\begin{figure}[p] |
| 456 |
\begin{center} |
| 457 |
<<arthritis1, fig=TRUE, height=6, width=7>>= |
| 458 |
<<arthritis>> |
| 459 |
@ |
| 460 |
\caption{\label{fig:arthritis} Extended mosaic display for arthritis data.} |
| 461 |
\end{center} |
| 462 |
\end{figure} |
| 463 |
|
| 464 |
To illustrate the use of diverging palettes in 2-class classification, we generate |
| 465 |
some artificial date from a mixture of two bivariate normal distributions with |
| 466 |
different means and covariance matrices. The data are generated using \pkg{mvtnorm} |
| 467 |
and collected in a data frame \code{ex1}: |
| 468 |
<<svm-data, echo=TRUE>>= |
| 469 |
library("mvtnorm") |
| 470 |
set.seed(123) |
| 471 |
x1 <- rmvnorm(75, mean = c(1.5, 1.5), |
| 472 |
sigma = matrix(c(1, 0.8, 0.8, 1), ncol = 2)) |
| 473 |
x2 <- rmvnorm(75, mean = c(-1, -1), |
| 474 |
sigma = matrix(c(1, -0.3, -0.3, 1), ncol = 2)) |
| 475 |
X <- rbind(x1, x2) |
| 476 |
ex1 <- data.frame(class = factor(c(rep("a", 75), |
| 477 |
rep("b", 75))), x1 = X[,1], x2 = X[,2]) |
| 478 |
@ |
| 479 |
We fit a support vector machine (SVM) with a radial basis function kernel to this data |
| 480 |
set, using the function \code{ksvm()} from \pkg{kernlab} |
| 481 |
<<svm-fit, echo=TRUE>>= |
| 482 |
library("kernlab") |
| 483 |
fm <- ksvm(class ~ ., data = ex1, C = 0.5) |
| 484 |
@ |
| 485 |
which can subsequently be easily visualized via |
| 486 |
<<svm, echo=TRUE, eval=FALSE>>= |
| 487 |
plot(fm, data = ex1) |
| 488 |
@ |
| 489 |
The resulting plot in Figure~\ref{fig:class} shows a heatmap with the fit of |
| 490 |
the SVM. The circles and triangles show the original observations, solid symbols correspond to |
| 491 |
the support vectors found. The shading underlying the plot visualizes the fitted decision |
| 492 |
values: values around 0 are on the decision boundary and are shaded in light gray, while |
| 493 |
regions that are firmly classified to one or the other class are shaded in full blue and |
| 494 |
red respectively. The palette used by the \code{plot()} method for \code{ksvm} objects |
| 495 |
cannot be easily modified---however, the colors employed are equivalent to |
| 496 |
\code{diverging_hcl(n, c = c(100, 0), l = c(50, 90), power = 1.3)}. |
| 497 |
|
| 498 |
\setkeys{Gin}{width=.7\textwidth} |
| 499 |
\begin{figure}[p] |
| 500 |
\begin{center} |
| 501 |
<<svm1, fig=TRUE, height=6, width=8, png=TRUE, pdf=FALSE>>= |
| 502 |
suppressWarnings( |
| 503 |
<<svm>> |
| 504 |
) |
| 505 |
@ |
| 506 |
\caption{\label{fig:class} SVM classification plot.} |
| 507 |
\end{center} |
| 508 |
\end{figure} |
| 509 |
|
| 510 |
|
| 511 |
\bibliography{color} |
| 512 |
|
| 513 |
\end{document} |