Customize

David Gerbing

Use the style() function to turn off text output for the following function calls.

style(quiet=TRUE)

Color Palettes

Many color models exist that describe colors in three dimensions. One of the more useful models defines the HCL color space. The colors are generated with the base R hcl() function, which has three parameters to specify a color: h for hue, c for chroma (saturation), and l for luminescence (brightness).

Qualitative Scales

The default output of getColors() is a color spectrum of 12 hcl colors presented in the order in which they are assigned to discrete levels of a categorical variable. For clarity in the following function call, the default value of the pal or palette parameter is explicitly set to its name, "hues".

getColors("hues")

To maintain equal brightness/intensity across the colors, all default colors generated by getColors() are based on chroma set at c=65 and luminescence set at l=60.

A color’s hue is based on integers around the color wheel from 0 to 360, starting at 0 for red. Use getColors() to display an hcl color wheel of 36 ordered hues around the wheel. The hcl color space numbers the hues from 0 to 360, with 0 arbitrarily indicating red.

getColors(n=36, shape="wheel", border="off")

Obtain deep, rich colors for an HCL qualitative palette by lowering chroma and luminance from their default values.

getColors(c=50, l=30)

The primary purpose of the color palettes is their application to data visualizations. Here, illustrate the deep, rich colors applied to a bar chart with the fill parameter that indicates the color of the bar interiors. Turn off text output with the quiet parameter set to TRUE.

d <- Read("Employee", quiet=TRUE)
BarChart(Dept, fill=getColors(c=50, l=30), quiet=TRUE)

Or, display pastels by setting chroma and luminescence high. Because the bars are so light colored, change the color of the displayed percentage on each bar with parameter values_color to a dark gray specified by "gray20" (on the gray scale, "gray0" is black and "gray100" is white).

In this example, generate the color palette separately from the call to BarChart(). When doing so, specify the number of colors to generate with the parameter n. When getColors() is called apart from BarChart(), the number of colors must be separately specified.

myColors <- getColors(c=90, l=80, n=5)
BarChart(Dept, fill=myColors, values_color="gray20", quiet=TRUE)

Sequential Scales

lessR provides pre-defined sequential color scales across the range of hues around the color wheel in 30 degree increments: "reds", "rusts", "browns", "olives", "greens", "emeralds", "turqoises", "aquas", "blues", "purples", "biolets", "magentas", and "grays".

To view the scales, use the function showPalettes(). Here it is commented out by the # sign in the first column because it writes the output to a pdf.

#showPalettes()

Generate a palette of 12 blue hues for the default value of parameter n set to 12..

getColors("blues")

Generate an hcl blue sequence with c=60 and vary l.

getColors("blues", c=60, l=c(30,80))

Vary chroma for a yellow hcl sequence. To do so, specify parameter c as a vector, here with values that vary from 20 to 90.

getColors("browns", c=c(20,90), l=60)

Here, illustrate getColors() in a data visualization. Stack three time series. According to the area_fill parameter, use Plot() to fill under each curve with a version of the sequential range "reds" with some transparency according to parameter trans. The data frame d2 is not the default data frame d, so explicitly specify with the data parameter.

d2 <- Read("StockPrice", quiet=TRUE)
Plot(date, Price, by=Company, stack=TRUE, area_fill="reds", trans=0.4, data=d2)

Divergent Scales

To create a divergent color palette, specify beginning and an ending color palettes, which provide values for the parameters pal and end_pal, where pal abbreviates palette. Here, generate colors from rust to blue.

getColors("rusts", "blues")

Add a custom value of chroma, c, to make more saturated than provided by the default value of chroma of 50.

getColors("rusts", "blues", c=75)

Other Scales

The family of color-friendly viridis scales are available: “viridis”, “cividis”, “magma”, “inferno”, and “plasma”.

getColors("viridis")

For something different, can use palettes based on many of Wes Anderson’s movies: “BottleRocket1”, “BottleRocket2”, “Rushmore1”, “Rushmore”, “Royal1”, “Royal2”, “Zissou1”, “Darjeeling1”, “Darjeeling2”, “Chevalier1”, “FantasticFox1”, “Moonrise1”, “Moonrise2”, “Moonrise3”, “Cavalcanti1”, “GrandBudapest1”, “GrandBudapest2”, “IsleofDogs1”, and “IsleofDogs2”.

getColors("Royal1")

To maximally differentiate colors based on hue, use the pre-defined palette “distinct”.

getColors("distinct")

Manual Specification of Colors

Consider an item on an attitude survey that is assessed on a 7-point Likert scale with responses that range from Strongly Disagree to Neutral to Strongly Agree. The goal is to create a bar chart with different sequential color palettes for the Disagree and Agree sides of the responses, with gray for the Neutral response. First, simulate some data for the item responses. Of course, these steps are not part of the usual data analysis because you would have your data, the responses to the item of interest.

values <- c(rep(1,2), rep(2,5), rep(3,4), rep(4,3), rep(5,6), rep(6,7), rep(7,8))
Responses <- sample(values, size=100, replace=TRUE)

LikertCats <- c("Strongly Disagree", "Disagree", "Slightly Disagree",
                "Neutral", "Slightly Agree", "Agree", "Strongly Agree")
Responses <- factor(Responses, levels=1:7, labels=LikertCats)

Create the two vectors, a sequential red scale for the Disagree responses and a sequential blue scale for the Agree responses. Indicate that each scale should encompass three colors with the parameter n.

red <- getColors("reds", n=3, c=c(75,35), l=c(27,63))
blue <- getColors("blues", n=3)

Another way to build the fill vector would be to examine the text output of getColors() for each palette and then copy and paste the displayed colors in either hexadecimal or rgb formats.

With the data, create a bar chart according to a vector of the seven colors.

BarChart(Responses, fill=c(red, "gray50", blue))

Visualization Themes

The style() function specifies broad color themes, as well as individual characteristics of a visualization. Changes based on a theme change are persistent across subsequent visualizations unless again modified.

In addition to the default theme "colors", other available themes are "lightbronze", "dodgerblue", "darkred", slatered, "gray", "gold", "darkgreen", "blue", "red", "rose", "slatered", "green", "purple", "sienna", "brown", "orange", "white", and "light".

d <- Read("Employee")

Obtain grayscale visualizations by setting the theme to "gray".

style("gray")
BarChart(Dept)

## >>> Suggestions
## BarChart(Dept, horiz=TRUE)  # horizontal bar chart
## BarChart(Dept, fill="reds")  # red bars of varying lightness
## PieChart(Dept)  # doughnut (ring) chart
## Plot(Dept)  # bubble plot
## Plot(Dept, stat="count")  # lollipop plot 
## 
## --- Dept --- 
## 
## Missing Values: 1 
## 
##                 ACCT   ADMN   FINC   MKTG   SALE    Total 
## Frequencies:       5      6      4      6     15       36 
## Proportions:   0.139  0.167  0.111  0.167  0.417    1.000 
## 
## Chi-squared test of null hypothesis of equal probabilities 
##   Chisq = 10.944, df = 4, p-value = 0.027

Here, invoke the "darkred" theme. With themes other than the default "colors" theme, the bars in a bar chart and boxes in a box charts are plotted with the same color. Do not need the text output, so set parameter quiet to TRUE.

style("darkred")
BarChart(Dept, quiet=TRUE)

Plot(Salary, by1=Dept, quiet=TRUE)

Return to the default colors for the theme parameter by either explicitly specifying, or just go with no specification, as in style().

Individual characteristics of a visualization can also be specified. Here change the background color of the visualization window. The theme parameter can also be attached to a single call to the visualization functions such as BarChart().

style(window_fill="aliceblue")
BarChart(Dept, theme="sienna")

## >>> Suggestions
## BarChart(Dept, horiz=TRUE)  # horizontal bar chart
## BarChart(Dept, fill="reds")  # red bars of varying lightness
## PieChart(Dept)  # doughnut (ring) chart
## Plot(Dept)  # bubble plot
## Plot(Dept, stat="count")  # lollipop plot 
## 
## --- Dept --- 
## 
## Missing Values: 1 
## 
##                 ACCT   ADMN   FINC   MKTG   SALE    Total 
## Frequencies:       5      6      4      6     15       36 
## Proportions:   0.139  0.167  0.111  0.167  0.417    1.000 
## 
## Chi-squared test of null hypothesis of equal probabilities 
##   Chisq = 10.944, df = 4, p-value = 0.027

View all modifiable individual characteristics with the show parameter set to TRUE.

style(show=TRUE)
## 
## Available Themes
## ----------------
## colors lightbronze dodgerblue darkred gray gold darkgreen blue red rose slatered green purple sienna brown orange white light 
## 
## Available Sub-themes
## --------------------
## default black wsj 
## 
## THEME
## theme ........ Theme color ....... darkred 
## sub_theme .... Sub-theme style ... default 
## 
## BACKGROUND
## window_fill .. Window fill color ......... aliceblue 
## panel_fill ... Panel fill color .......... grey99 
## panel_color .. Panel border color .......  gray45 
## panel_lwd .... Panel border line width ..  1.0 
## panel_lty .... Panel border line type ...  solid 
## 
## DATA OBJECTS
## bar_fill ......... Bar fill color ............ 138 0 0 230 
## trans_bar_fill ... Bar fill transparency ..... 0.10 
## bar_color ........ Bar border color .......... 132 150 175 255 
## values ........... Form of bar or pie values . % 
## values_color ..... Color of bar or pie values . white 
## values_size ..... Size of values on bars, pie  NULL 
## values_digits .... Decimal digits on bars, pie  NULL 
## values_position .... Position of values ...... .. in 
## 
## pt_fill .......... Point fill color .......... darkred 
## trans_pt_fill .... Point fill transparency ..  0.00 
## pt_color ......... Point border color .......  80 0 0 255 
## out_fill ......... Outlier point fill .......  firebrick4 
## out_color ........ Outlier point color ......  firebrick4 
## out2_fill ........ Extreme outlier point fill  firebrick2 
## out2_color ....... Extreme outlier point color firebrick2 
## violin_fill ...... Violin fill color ......... 116 133 151 90 
## violin_color ..... Violin border color ....... gray15 
## box_fill ......... Boxplot fill color ........ 138 0 0 159 
## box_color ........ Boxplot border color ...... gray15 
## fit_color ........ Fit line_color ...........  92 64 50 255 
## se_fill .......... Stnd error fill color ....  138 0 0 40 
## ellipse_fill ..... Ellipse fill color .......  138 0 0 15 
## ellipse_color .... Ellipse border color .....  138 0 0 199 
## ellipse_lwd ...... Ellipse border width .....  1.00 
## bubble_text_color  Bubble text color ......... black 
## segment_color .... Line segment color ........ darkred 
## heat ............. Heat map color ............ gray30 
## 
## AXES
## axis_color ..... Color of axes .............. gray15 
## axis_x_color ... Color of x-axis ............ NULL 
## axis_y_color ... Color of y-axis ............ NULL 
## axis_lwd ....... Axis line width ............ 1.0 
## axis_x_lwd ..... Axis line width ............ NULL 
## axis_y_lwd ..... Axis line width ............ NULL 
## axis_lty ....... Line type of axes .......... solid 
## axis_x_lty ..... Line type of x-axis ........ NULL 
## axis_y_lty ..... Line type of y-axis ........ NULL 
## axis_cex ....... x and y axis text size ........... 0.75 
## axis_x_cex ..... x-axis text size ................. NULL 
## axis_y_cex ..... y-axis text size ................. NULL 
## axis_text_color  x and y axis values text color ... gray20 
## axis_x_text_color  x-axis values text color ....... NULL 
## axis_y_text_color  y-axis values text color ....... NULL 
## rotate_x ....... Rotation of x axis text .......... 0.00 
## rotate_y ....... Rotation of y axis text .......... 0.00 
## offset ......... Offset of values text from axis .. 0.50 
## 
## LABELS
## lab_color ...... Color of axis labels ............. gray15 
## lab_x_color .... Color of x-axis label ............ NULL 
## lab_y_color .... Color of y-axis label ............ NULL 
## lab_cex ........ Size of axis labels .............. 0.98 
## lab_x_cex ...... Size of x-axis labels ............ NULL 
## lab_y_cex ...... Size of y-axis labels ............ NULL 
## main_color ..... Color of plot label .............. gray15 
## main_cex ....... Size of plot title ............... 1.00 
## 
## GRID LINES
## grid_color .... Grid color ...................... 222 217 205 255 
## grid_x_color .. Grid color, vertical ............ NULL 
## grid_y_color .. Grid color, horizontal .......... NULL 
## grid_lwd ...... Grid line width ................. 0.5 
## grid_x_lwd .... Grid line width, vertical ....... NULL 
## grid_y_lwd .... Grid line width, horizontal ..... NULL 
## grid_lty ...... Grid line type .................. solid 
## grid_x_lty .... Grid line type, vertical ........ NULL 
## grid_y_lty .... Grid line type, horizontal ...... NULL 
## 
## TRELLIS STRIP
## strip_fill ...... Trellis strip fill color ..... 138 0 0 55 
## strip_color ..... Trellis strip border color ... 80 0 0 255 
## strip_text_color  Trellis strip text color ..... 80 0 0 255 
## 
## ANNOTATION
## add_fill  .. Fill color of annotated figures .. gray20 
## add_trans .. Fill transparency ................ 0 
## add_color .. Color of annotated lines ......... gray30 
## add_cex  ... Size of annotated text ........... 0.75 
## add_lwd  ... Line width of annotated lines .... 0.5 
## add_lty  ... Line type of annotated lines ..... solid 
## 
## NON-GRAPHICAL
## quiet ..... Suppress console output for many functions .. FALSE 
## brief ..... Reduce console output for many functions .... FALSE 
## suggest ... Suggestions for enhanced input .............. TRUE 
## note ...... Notes for enhanced input .................... TRUE 
## width ..... Column width ................................ 120 
## n_cat ..... Largest number of unique, equally spaced
##             integer values of a variable for which the
##             variable will be analyzed as categorical .... 1
## theme set to "colors"

Full Manual

Use the base R help() function to view the full manual for getColors() or style(). Simply enter a question mark followed by the name of the function.

?getColors
?style