Skip to contents

plot_cube plots one or more colors x as points in a 3-dimensional cube whose axes correspond to the red, green, and blue values of the RGB color model (ranging from 0 to 255).

Usage

plot_cube(
  x,
  type = "p",
  col_points = NA,
  cex_points = 2,
  pch = 21,
  col_brd = grey(0.25, 1),
  n_boxes = 20,
  shade = FALSE,
  ref_lines = FALSE,
  col_line = NA,
  lwd_line = 3,
  line_connect = "path",
  col_fill = NA,
  lwd_outline = 1,
  fill_alpha = 0.75,
  fill_res = 20,
  shadows = "none",
  col_shadow = grey(0.7, 0.9),
  show_corners = TRUE,
  col_cube = grey(0.5, 1),
  grid = FALSE,
  grid_faces = FALSE,
  grid_inner = FALSE,
  grid_at = 2,
  col_grid = grey(0.6, 0.6),
  col_grid_inner = grey(0.8, 0.5),
  axis_labels = c("R", "G", "B"),
  axis_ticks = TRUE,
  n_ticks = 5,
  cex_axis = NA,
  labels = "names",
  theta = -30,
  phi = 15,
  main = "Colors in 3D RGB space",
  cex_lbl = 0.85,
  ...
)

Arguments

x

A (character) vector of colors to be plotted, specified as R color names (e.g., "deepskyblue"), HEX values (e.g., "#59C7EBFF"), or any mixture thereof. Color palettes (e.g., pal_unikn) are also accepted.

Alternatively, x may be a list of color groups, which provides control over which colors are linked as lines or planes (c() structures list elements into groups). Each group is drawn according to its size:

  • a group of 1 color is shown as an (unlinked) point;

  • a group of 2 colors is connected by a line;

  • a group of 3 or more colors spans a filled plane.

For example, plot_cube(list(Seeblau, c(Pinky, Seegruen), c("red", "green", "blue"))) shows 1 point, 1 line, and 1 plane in the same cube. In this grouped mode, all defining colors are also marked as points (as for type = "b"), and the type and line_connect arguments are ignored (as the geometry follows from each group's size). A single data frame palette (e.g., pal_unikn) is not treated as a list of groups, but as one flat vector of colors. To connect 3 or more colors by lines (rather than a plane), use a (flat) vector x with type = "l"/"b" instead.

type

Type of geometric element to draw from the colors x:

  • "p": Plot colors as points (default);

  • "l": Connect colors by lines (a path through RGB space);

  • "b": Draw both points and lines;

  • "plane": Span a plane (filled polygon) through the colors (requires 3 or more colors; 3 colors yield a triangle);

  • "box": Plot colors as 3D boxes (one box per color), centered on each color's RGB coordinate (see n_boxes for size).

By default, connecting lines and plane fills are shown as color gradients (i.e., each location is shown in the color of the corresponding RGB mixture), to illustrate the additive mixing of colors in RGB space. In all cases, only the colors x provided (i.e., the points that define the line or plane) are labeled by default (see labels). Default: type = "p".

col_points

Color(s) used for the plotted points. Default: col_points = NA uses each point's own color (i.e., every color in x is plotted in that color). Providing a color (or color vector) overrides this, so that all points are shown in the specified color(s).

cex_points

Scaling factor for the size of points. Default: cex_points = 2.

pch

Plotting symbol for points (see points). Default: pch = 21 (a fillable circle).

col_brd

Border color of points (for fillable symbols, i.e., pch in 21:25) and of box edges (for type = "box"). Default: col_brd = grey(.25, 1) (dark grey). For type = "box", if col_brd is not specified, each box's edge grey is chosen automatically to contrast with its own color (dark colors get a light grey edge, bright colors a dark grey edge); specifying col_brd uses that one color for all box edges.

n_boxes

Number of boxes per axis (for type = "box"): Each box has a side length of 255 / n_boxes (in RGB units), so larger values yield smaller boxes (that overlap less often). Default: n_boxes = 20 (i.e., box side = 12.75).

Note: Boxes are axis-aligned (their edges run parallel to the RGB axes). Under the default perspective projection of persp (d = 1), however, parallel 3D lines converge toward vanishing points, so a given axis direction projects to a slightly different 2D angle at different locations within the cube. As a result, boxes in the interior may appear rotated relative to the enclosing cube's outer edges (a projection effect). To reduce it, pass a larger d (e.g., d = 8, via ...) for a (near-)orthographic view, in which each box's edges run parallel to the enclosing cube (see d in persp).

shade

Boolean: Shade the faces of each box (for type = "box") to enhance the 3D effect? When shade = TRUE, the most viewer-facing face (i.e., the one actually seen) shows the color's true RGB value, while faces turned away from the viewer are only gently darkened (to at most ~15%), so a box's visible color stays faithful while gaining some depth. (The shading is thus based on the viewing direction, not a fixed "top" face, so it works at any theta/phi.) The default shade = FALSE draws every face in the true color, relying on the box's edges (col_brd) to convey its 3D shape. Default: shade = FALSE.

ref_lines

Boolean: Draw a vertical reference line from each point down to the floor (R-by-G base plane) of the cube (to ease judging a point's height on the B/blue axis)? Default: ref_lines = FALSE.

col_line

Color of connecting lines (for type = "l" or "b") and of the plane outline (for type = "plane"). Default: col_line = NA (i.e., show lines and plane outlines as a color gradient of the mixtures between consecutive colors).

lwd_line

Line width of connecting lines (for type = "l" or "b"). Raise value for thicker, more visible lines. Default: lwd_line = 3.

line_connect

How to connect 3 or more colors by lines (for type = "l" or "b"):

  • "all": Connect all pairs of colors (i.e., n * (n - 1) / 2 lines, a complete graph);

  • "pairs": Connect only disjoint consecutive pairs of colors, i.e., (1st, 2nd), (3rd, 4th), etc. (yielding floor(n / 2) lines; if n is odd, the final color remains unpaired, but is still shown as a point);

  • "path": Connect only consecutive colors (i.e., n - 1 lines, tracing the palette in its given order).

For 2 colors, all 3 options yield the same single line. (The value "pairwise" is accepted as an alias for "all".) Default: line_connect = "path".

col_fill

Fill color for the plane (for type = "plane"). Default: col_fill = NA (i.e., fill the plane with a color gradient showing the gamut of all mixtures of the defining colors).

lwd_outline

Line width of plane outlines (for type = "plane" and planes of grouped input), drawn along the outer edges of a plane only. Use lwd_outline = 0 to draw no outline. Default: lwd_outline = 1.

fill_alpha

Opacity of the plane fill (in range [0, 1]). Default: fill_alpha = .75.

fill_res

Resolution of color gradients (i.e., the number of line segments per connection, and the granularity of the plane tessellation). Higher values yield smoother gradients (but slower plotting). Default: fill_res = 20.

shadows

Project each point onto one or more faces of the cube (as "shadow" points, to aid judging its 3D location). Options (one or more of which may be combined) are:

  • "floor": Project onto the R-by-G plane (i.e., set B = 0);

  • "back": Project onto the R-by-B plane (i.e., set G = 0);

  • "side": Project onto the G-by-B plane (i.e., set R = 0);

  • "all": Project onto all 3 faces;

  • "none": Show no shadows.

Each shadow is a smaller marker on the corresponding face, linked to its point by a dotted connector. As a projection drops one color channel, a shadow also shows the color with that primary removed. Default: shadows = "none".

col_shadow

Color of the shadow markers. Default: col_shadow = grey(.70, .9) (a neutral grey).

Setting col_shadow = NA shows each shadow in its own projected color. Projecting a point onto a face sets the one color channel perpendicular to that face (i.e., the RGB axis not spanned by the face's plane) to zero: a "floor" shadow sets B = 0, a "back" shadow sets G = 0, and a "side" shadow sets R = 0 (see shadows). As one channel is dropped, a projected shadow color thus systematically differs from its source color: e.g., "violet" (238, 130, 238) casts an orange (238, 130, 0) floor shadow, a magenta (238, 0, 238) back shadow, and a blue (0, 130, 238) side shadow.

show_corners

Boolean option to mark the 8 corners of the cube by points in their corresponding colors (i.e., black, white, and the 6 primary/secondary colors RGB/CMY). Default: show_corners = TRUE.

col_cube

Color for the edges and axes of the cube. Default: col_cube = grey(.50, 1).

grid

Boolean convenience switch to enable a default reference-grid combination (akin to the grid of seecol). grid = TRUE turns on the inner/volume grid (grid_inner = TRUE), which in turn draws the face grid on all 6 faces (grid_faces = "all", so the interior lines end on the faces), at the default spacing (grid_at); grid = FALSE (default) shows no grid. For finer control, set grid_faces, grid_inner, and grid_at directly. Default: grid = FALSE.

grid_faces

Draw reference grid lines on the cube's faces, to ease judging a point's 3D location. Options are:

  • FALSE: Show no face grid (default);

  • "base": Grid on the 3 faces meeting at the black corner (floor + 2 near walls);

  • "all": Grid on all 6 faces (a full box grid).

Line positions are set by grid_at. When grid_inner is enabled, grid_faces defaults to "all" (so the interior lines terminate on the faces), unless set explicitly. Default: grid_faces = FALSE.

grid_inner

Boolean to draw reference grid lines across the cube's volume (interior lines connecting opposite faces: front-to-back, top-to-bottom, and left-to-right), as a 3D orientation aid. Line positions are set by grid_at; best used sparsely (small grid_at), as each position adds many interior lines. (Enabling this also sets grid_faces = "all" by default; see above.) Default: grid_inner = FALSE.

grid_at

Positioning of the grid lines (for grid_faces and grid_inner) along each axis. Options are:

  • an integer k: Dissect each axis into k equal sub-sections (e.g., 2 = halves/mid-point (default), 3 = thirds, 4 = quarters);

  • "ticks": At the current axis-tick positions (see n_ticks);

  • a numeric vector: Use custom positions (in 0:255).

(The endpoints 0 and 255 are always omitted, as they are the cube's edges.) Default: grid_at = 2 (sparse).

col_grid

Color of the face grid lines (grid_faces). Default: col_grid = grey(.60, .60) (a thin, transparent, darker grey).

col_grid_inner

Color of the inner (volume) grid lines (grid_inner). Default: col_grid_inner = grey(.80, .50) (a thin, transparent, lighter grey) — lighter than col_grid, so the face grid reads as the stronger reference.

axis_labels

Titles for the 3 cube axes (R, G, and B). Options are:

  • a character vector of length\ 3: Use these as axis titles;

  • "short" (or "rgb"): Use c("R", "G", "B") (default);

  • "long": Use c("Red", "Green", "Blue");

  • NA (or "no"): Show no axis titles.

Default: axis_labels = c("R", "G", "B").

axis_ticks

Draw tick marks and numeric value labels (in range 0 to 255) along the 3 cube axes? Either a Boolean value, or a numeric vector specifying custom tick positions (in 0:255). Default: axis_ticks = TRUE.

n_ticks

Number of (equally spaced) ticks per axis (only used when axis_ticks = TRUE). Default: n_ticks = 5 (i.e., ticks at 0, 64, 128, 191, 255).

cex_axis

Scaling factor for axis titles and tick values. Default: cex_axis = NA (i.e., use cex_lbl).

labels

Type(s) of labels for the points. One or more of the following label types may be specified (and are shown on separate lines when combined):

  • "names": Show the names of x (or the color values, if no names are available);

  • "rgb": Show each point's RGB triplet (as (R, G, B));

  • "hex": Show each point's HEX value (e.g., "#59C7EB").

Thus, labels = c("names", "rgb", "hex") shows all 3 representations of each color in the same cube. Setting labels = NA (or "no") shows no labels, and passing a character vector (of custom labels) shows these labels instead. Default: labels = "names".

theta

Horizontal rotation angle (azimuth, used by persp). Default: theta = -30.

phi

Vertical rotation angle (colatitude, used by persp). Default: phi = 15.

main

Main plot title. Default: main = "Colors in 3D RGB space".

cex_lbl

Scaling factor for text labels (axes, titles, point labels). Default: cex_lbl = .85.

...

Other (graphical) parameters passed to persp (e.g., d, expand). In particular, d controls the strength of the perspective projection: the default (d = 1) shows clear perspective, whereas larger values (e.g., d = 8) approach an orthographic view (relevant for type = "box"; see n_boxes).

Value

A projection matrix (of class returned by persp) that maps 3D coordinates to the 2D plotting plane (returned invisibly).

Details

Each color is converted into its RGB coordinates (via col2rgb) and shown as a point at the corresponding location in the color cube. Points are filled with the color they represent, so that the location of a point in the cube illustrates the additive composition of its color from the three primary colors (Red, Green, and Blue).

plot_cube uses base graphics (i.e., persp and trans3d) and requires no additional package dependencies.

See also

seecol for viewing and comparing color palettes; usecol for using color palettes; newpal for defining new color palettes; col2rgb for converting colors to RGB values.

Other color functions: ac(), demopal(), grepal(), newpal(), seecol(), shades_of(), simcol(), usecol()

Examples

# A. Basics: ------ 

plot_cube("tomato")                        # a single color

plot_cube(c("red", "green", "blue"))       # primary RGB colors

plot_cube(c("black", "grey50", "white"))   # grey diagonal


# Showing color palettes:
plot_cube(pal_unikn)       # a bi-polar color palette

plot_cube(pal_unikn_pref)  # a categorical color palette


# Reduced color options:
col_mix <- c("grey50", "deepskyblue", "forestgreen", "gold", "orchid", "tomato")
plot_cube(col_mix, show_corners = FALSE)    # hide corner colors

plot_cube(pal_unikn, col_points = "black")  # all points in 1 color


# Labeling points (by names, RGB or HEX values, or combinations):
col_4 <- c("red3", "green4", "gold", "violet")
plot_cube(col_4)                                     # names (default)

plot_cube(col_4, labels = "hex")                     # HEX values

plot_cube(col_4, labels = "rgb")                     # RGB values

plot_cube(col_4, labels = c("names", "hex", "rgb"))  # all 3 types

plot_cube(col_4, labels = "no")                      # no labels


# Connecting points by lines (type = "l" or "b"):
plot_cube(c("black", "white"), type = "l")          # line (grey diagonal)

plot_cube(c("red", "blue"), type = "b")            # line with points (red-blue gradient)


# Changing the viewing angle:
plot_cube(pal_unikn, theta = 40, phi = 40)


# With >2 colors: All pairs, disjoint pairs, or a connecting path (default):
plot_cube(col_4, type = "l", line_connect = "all")       # all pairs

plot_cube(col_4, type = "b", line_connect = "pairs")     # disjoint pairs (and points)

plot_cube(pal_unikn, type = "b", line_connect = "path")  # connecting path (in palette order)


# Using line_connect = "pairs" to link disjoint color pairs, e.g., to show
# the 3 complementary RGB/CMY pairs (each line crosses the grey cube center):
plot_cube(c("red", "cyan", "green", "magenta", "blue", "yellow"), type = "b", 
          line_connect = "pairs", grid = TRUE, main = "Complementary colors")


# Spanning a plane through 3 colors (type = "plane"):
plot_cube(c("red", "green", "blue"), type = "plane")  # gamut of 3 primary RGB colors

plot_cube(c("cyan", "yellow", "magenta"),             # gamut of 3 opposite RGB colors
             type = "plane", labels = c("names", "rgb"))


# Colors as 3D boxes (type = "box"): Box faces show each color's RGB value
plot_cube(c("cyan3", "gold", "grey80", "grey20", "skyblue", "tomato"), type = "box") # point boxes

plot_cube(pal_unikn, type = "box", grid = TRUE)       # a palette as boxes

# plot_cube(pal_unikn, type = "box", n_boxes = 10)    # fewer, larger boxes
# plot_cube(pal_unikn, type = "box", shade = TRUE)    # shaded faces (subtle 3D effect)
# plot_cube(pal_unikn, type = "box", d = 10)          # near-orthographic (edges parallel)

# Grouped colors: Passing a list combines colors (by group size):
# Each c() group becomes a point (1 color), line (2), or plane (3+):
plot_cube(list(Seeblau, c(Pinky, Seegruen),   # 1 point, 1 line, and 
               c("red", "green", "blue")))    # 1 plane


# A lone point, a line, and a plane (with the defining points marked):
plot_cube(list("gold",                                # unlinked point
               c("black", "white"),                   # b/w diagonal (line)
               c("cyan", "magenta", "yellow")))       # CMY gamut (plane)


# Two separate lines (e.g., 2 complementary RGB/CMY pairs):
plot_cube(list(c("red", "cyan"), c("green", "magenta")), lwd_line = 5, grid = TRUE)


# Groups may themselves be palettes (data frames are flattened per group):
plot_cube(list(pal_seeblau, pal_pinky))              # 2 palette planes


# Shadow points (projections onto cube faces):
plot_cube(c("tomato", "deepskyblue"), shadows = "floor")  # on the floor (B = 0)

plot_cube(pal_seeblau, shadows = "all")                   # on all 3 faces

plot_cube(c("grey20", "grey50", "grey80"),                # show shadows in projected colors
          shadows = "all", col_shadow = NA) 

          
# Axis options (titles and value ticks):
plot_cube(pal_unikn, axis_labels = "long")           # long axis titles

plot_cube(pal_unikn, n_ticks = 11)                   # finer axis scale (0, 25.5, ...)

plot_cube(pal_unikn, axis_ticks = c(0, 128, 255))    # custom tick positions

plot_cube(pal_unikn, axis_ticks = FALSE)             # no ticks (titles only)

plot_cube(pal_unikn, axis_labels = "no", axis_ticks = FALSE)  # bare cube


# Grid options (to aid judging 3D locations): 
# Both reference grids improve orientation, but only when used sparsely.

# a. Default of grid = TRUE (splits cube into octants): 
col_mix <- c("grey50", "deepskyblue", "forestgreen", "gold", "orchid", "tomato")
plot_cube(col_mix, grid = TRUE)


# b. Both grids at thirds (more structure, but quickly gets dense): 
plot_cube(col_mix, grid = TRUE, grid_at = 3)


# c. Both grids at rare axis ticks (to align grid with tick labels):
plot_cube(col_mix, grid = TRUE, grid_at = "ticks", n_ticks = 4)


# d. Face grid only (often suffices, as inner grid may not be needed):
plot_cube(pal_unikn, grid_faces = "base", n_ticks = 5, grid_at = "ticks")



# B. Basic demonstrations of the 3D color cube (RGB/CMY): ------

# 1. The 8 color corners of the RGB cube (b/w + RGB + CMY):
col_8 <- c("black", "white", "red", "cyan", "blue", "yellow", "green", "magenta")
plot_cube(col_8, theta = -30, phi = 30, labels = c("names", "rgb", "hex"),
          main = "The 6 corners of the RGB cube")


# 2. The main black-grey-white diagonal of the cube (with all shades of grey):
greys <- paste("grey", seq(0, 100, length.out = 5))  # 5 shades of grey (black to white)
plot_cube(greys, type = "b", line_connect = "path", 
          lwd_line = 5, labels = c("names", "hex", "rgb"),
          phi = 20, theta = -30, main = "5 shades of grey (on b/w diagonal)")


# 3. Complementarity of the RGB and CMY color systems:
#    Each CMY secondary (cyan, magenta, yellow) is the complement of an RGB
#    primary (red, green, blue): It occupies the opposite corner of the cube,
#    and a color and its complement mix to grey (at the cube's center).

# Show 6 color corners (RGB vs. CMY):
col_6 <- c("red", "cyan", "blue", "yellow", "green", "magenta")
plot_cube(col_6, labels = c("names", "rgb"),
          main = "RGB vs CMY: 6 complementary corners of the cube")

          
# Show all 4 cube diagonals:
plot_cube(col_8, type = "b", line_connect = "pairs", lwd_line = 5,
          grid = TRUE, phi = 30, theta = 0, main = "The 4 main diagonals")

            
# Show 2 triangular planes:
plot_cube(c("red", "blue", "green"), type = "plane", main = "The RGB plane")

plot_cube(c("cyan", "yellow", "magenta"), type = "plane", main = "The CYK plane")



# C. Illustrating colors and color palettes (unikn): ------

# 0. Collect main unikn colors (as a vector):
col_4 <- c(Seeblau, Pinky, Seegruen, Bordeaux)
col_8 <- c(col_4, Petrol, Karpfenblau, Peach, Grau)

# 1. Main unikn colors as (labeled) points:
plot_cube(col_8, grid = TRUE, main = "The main unikn colors in RGB space")


# 2. The default unikn palette (black - Seeblau shades - white) as a path:
plot_cube(pal_unikn, type = "b", line_connect = "path", lwd_line = 4,
          main = "A bi-polar palette (pal_unikn)")

plot_cube(pal_unikn_pref, type = "b", line_connect = "path", lwd_line = 4,
          main = "A categorical color palette (pal_unikn_pref)")

          
# 2. A color palette's shade ramp as a connecting path:
plot_cube(pal_pinky, type = "b", line_connect = "path", lwd_line = 5,
          theta = -40, phi = 20, shadows = "all", main = "The path of pal_pinky")

plot_cube(usecol(c(rev(pal_seegruen), "white", pal_bordeaux)), type = "b", 
          line_connect = "path", lwd_line = 5, main = "A custom color gradient")

          
# 4. Pairwise relations among main colors:
plot_cube(col_4, type = "plane", shadows = "floor", theta = 0, phi = 0, 
          main = "Pairwise relations of 4 key colors")
#> plot_cube: >3 colors connected as a filled polygon (may be non-planar).


# 5. The gamut (plane) of mixtures spanned by 3 main colors:
plot_cube(c(Seeblau, Pinky, Seegruen), type = "plane",
          theta = 10, phi = 20, main = "Gamut of 3 key colors")