| Title: | Extended Tools for Continuous Legends, Polygon Manipulation, and Visual Display of Categorical Data |
|---|---|
| Description: | Annotate plots with legends for continuous variables and colour spectra using the base graphics plotting tools; and manipulate irregular polygons. Includes palettes for colour-blind viewers. |
| Authors: | Martin R. Smith [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-5660-1727>), Martin Krzywinski [ctb] (ORCID: <https://orcid.org/0009-0005-5785-3631>, Palettes) |
| Maintainer: | Martin R. Smith <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.4.0 |
| Built: | 2026-05-18 10:07:17 UTC |
| Source: | https://github.com/ms609/plottools |
Colour palettes whose elements are chosen to be distinguishable by colour blind audiences.
cbPalette8 cbPalette12 cbPalette15 cbPalette24 altPalette8 altPalette12 altPalette15 altPalette24cbPalette8 cbPalette12 cbPalette15 cbPalette24 altPalette8 altPalette12 altPalette15 altPalette24
Named character vectors listing RGB values for each colour.
An object of class character of length 8.
An object of class character of length 12.
An object of class character of length 15.
An object of class character of length 24.
An object of class character of length 8.
An object of class character of length 12.
An object of class character of length 15.
An object of class character of length 24.
Palettes are named according to the number of distinct colours they contain.
Each palette is arranged in three groups, intended to achieve perceptual luminance uniformity for deuteranopic viewers (see source for details), from dark to light.
For each cbPalette, an altPalette provides a hue selected from the many
that are perceptually indistinguishable for deuteranopic viewers.
https://mk.bcgsc.ca/colorblind/palettes.mhtml
palette.colors(8) provides an eight-colour Okabe-Ito colour-blind palette.
par(mfrow = c(4, 1), mar = rep(1, 4)) data("cbPalette8") plot.new() plot.window(xlim = c(1, 8), ylim = c(-1, 1)) text(1:8, 1, col = cbPalette8) points(1:8, rep(0, 8), bg = cbPalette8, col = altPalette8, pch = 22, cex = 2) data("cbPalette12") plot.new() plot.window(xlim = c(1, 12), ylim = c(-1, 1)) text(1:12, 1, col = cbPalette12) points(1:12, rep(0, 12), bg = cbPalette12, col = altPalette12, pch = 22, cex = 2) data("cbPalette15") plot.new() plot.window(xlim = c(1, 15), ylim = c(-1, 1)) text(1:15, 1, col = cbPalette15) points(1:15, rep(0, 15), bg = cbPalette15, col = altPalette15, pch = 22, cex = 2) plot.new() plot.window(xlim = c(1, 25), ylim = c(-1, 1)) text(1:24, 1, col = cbPalette24) points(1:24, rep(0, 24), bg = cbPalette24, col = altPalette24, pch = 22, cex = 2)par(mfrow = c(4, 1), mar = rep(1, 4)) data("cbPalette8") plot.new() plot.window(xlim = c(1, 8), ylim = c(-1, 1)) text(1:8, 1, col = cbPalette8) points(1:8, rep(0, 8), bg = cbPalette8, col = altPalette8, pch = 22, cex = 2) data("cbPalette12") plot.new() plot.window(xlim = c(1, 12), ylim = c(-1, 1)) text(1:12, 1, col = cbPalette12) points(1:12, rep(0, 12), bg = cbPalette12, col = altPalette12, pch = 22, cex = 2) data("cbPalette15") plot.new() plot.window(xlim = c(1, 15), ylim = c(-1, 1)) text(1:15, 1, col = cbPalette15) points(1:15, rep(0, 15), bg = cbPalette15, col = altPalette15, pch = 22, cex = 2) plot.new() plot.window(xlim = c(1, 25), ylim = c(-1, 1)) text(1:24, 1, col = cbPalette24) points(1:24, rep(0, 24), bg = cbPalette24, col = altPalette24, pch = 22, cex = 2)
Convert R colour to hexadecimal representation.
Col2Hex(col, alpha = FALSE)Col2Hex(col, alpha = FALSE)
col |
vector of any of the three kinds of R color specifications,
i.e., either a color name (as listed by |
alpha |
logical value indicating whether the alpha channel (opacity) values should be returned. |
Martin R. Smith ([email protected])
Col2Hex(1:3) Col2Hex(c("peachpuff", "blue"), TRUE)Col2Hex(1:3) Col2Hex(c("peachpuff", "blue"), TRUE)
Geometry functions for irregular polygons.
PolygonArea(x, y = NULL, positive = TRUE) PolygonCentre(x, y = NULL) PolygonCenter(x, y = NULL) GrowPolygon(x, y = NULL, buffer = 0)PolygonArea(x, y = NULL, positive = TRUE) PolygonCentre(x, y = NULL) PolygonCenter(x, y = NULL) GrowPolygon(x, y = NULL, buffer = 0)
x, y
|
Vectors containing the coordinates of the vertices of the polygon. |
positive |
If vertices are specified in an anticlockwise direction,
the polygon will be treated as a hole, with a negative area, unless
|
buffer |
Numeric specifying distance by which to grow polygon. |
PolygonArea() returns the area of the specified polygon.
PolygonCentre() returns a single-row matrix containing the
x and y coordinates of the geometric centre of the polygon.
GrowPolygon() returns coordinates of the vertices of polygon
after moving each vertex buffer away from the polygon's centre.
PolygonArea(): Calculate the area of an irregular polygon
PolygonCentre(): Locate the centre of a polygon
GrowPolygon(): Enlarge a polygon in all directions
Martin R. Smith ([email protected])
x <- c(-3, -1, 6, 3, -4) y <- c(-2, 4, 1, 10, 9) plot(x, y, frame.plot = FALSE) polygon(x, y) PolygonArea(x, y) points(PolygonCentre(x, y), pch = 3, cex = 2) polygon(GrowPolygon(x, y, 1), border = "darkgreen", xpd = NA # Allow drawing beyond plot border ) # Negative values shrink the polygon polygon(GrowPolygon(x, y, -1), border = "red")x <- c(-3, -1, 6, 3, -4) y <- c(-2, 4, 1, 10, 9) plot(x, y, frame.plot = FALSE) polygon(x, y) PolygonArea(x, y) points(PolygonCentre(x, y), pch = 3, cex = 2) polygon(GrowPolygon(x, y, 1), border = "darkgreen", xpd = NA # Allow drawing beyond plot border ) # Negative values shrink the polygon polygon(GrowPolygon(x, y, -1), border = "red")
Prints an annotated vertical bar coloured according to a continuous palette.
SpectrumLegend( x = "topright", ..., palette, legend, lty = 1, lwd = 4, bty = "o", adj = if (horiz) c(0.5, 0.5) else c(0, 0.5), horiz = FALSE, lend = "butt", cex = 1, seg.len = 1 ) SizeLegend( x = "topright", ..., legend = character(0), width = c(0, 1), palette = par("col"), scale = c("pch", "lwd"), lty = 0, lwd = 4, bty = "o", adj = if (horiz) c(0.5, 0.5) else c(0, 0.5), horiz = FALSE, lend = "butt", cex = 1, seg.len )SpectrumLegend( x = "topright", ..., palette, legend, lty = 1, lwd = 4, bty = "o", adj = if (horiz) c(0.5, 0.5) else c(0, 0.5), horiz = FALSE, lend = "butt", cex = 1, seg.len = 1 ) SizeLegend( x = "topright", ..., legend = character(0), width = c(0, 1), palette = par("col"), scale = c("pch", "lwd"), lty = 0, lwd = 4, bty = "o", adj = if (horiz) c(0.5, 0.5) else c(0, 0.5), horiz = FALSE, lend = "butt", cex = 1, seg.len )
x, horiz, adj, seg.len, ...
|
Additional parameters to |
palette |
Colour palette to depict.
Specify either a vector of colours, or a function such that |
legend |
Character vector with which to label points on |
lwd, lty, lend
|
Additional parameters to |
bty |
Character specifying the type of box to be drawn around the
legend. The allowed values are |
cex |
Character expansion factor relative to current |
width |
Vector of length two specifying width of legend bar at base and top. |
scale |
Character string specifying whether |
col |
Colour used for the width bar. |
This convenience function is not yet very customizable; do file a GitHub issue if you would value additional functionality.
Note that the bg parameter to specify the background colour for the legend
box is not presently supported in vertical legends.
For use in vertical legends, open a GitHub issue.
A list, returned invisibly, with components:
rect A list with components:
w, h: positive numbers giving width and height of the
legend's box.
left, top: x and y coordinates of the upper left corner of the
box.
text: A list with components x, y, numeric vectors of length
length(legend), giving the x and y coordinates of the legend's text(s).
Martin R. Smith ([email protected])
plot(0:1, 0:1, type = "n", frame.plot = FALSE, xlab = "x", ylab = "y") SpectrumLegend("bottomright", legend = c("Bright", "Middle", "Dark"), palette = heat.colors(32L), lwd = 5, inset = 0.05, # Inset from plot margin title = "Brightness") SpectrumLegend("topright", horiz = TRUE, legend = seq(1, 9, by = 2), palette = 1:8) SizeLegend( "topleft", inset = 0.05, width = c(0, 2), title = "Width", legend = c("max", ".", ".", "min"), palette = topo.colors, # Associate with a colour scale y.intersp = 1.5 # Vertical space between labels (also moves title) ) SizeLegend( "bottomleft", horiz = TRUE, width = c(4, 1), legend = c("Thick", "Thin"), palette = "darkred", inset = 0.06 # Make space for the bar. # A future release may calculate this automatically )plot(0:1, 0:1, type = "n", frame.plot = FALSE, xlab = "x", ylab = "y") SpectrumLegend("bottomright", legend = c("Bright", "Middle", "Dark"), palette = heat.colors(32L), lwd = 5, inset = 0.05, # Inset from plot margin title = "Brightness") SpectrumLegend("topright", horiz = TRUE, legend = seq(1, 9, by = 2), palette = 1:8) SizeLegend( "topleft", inset = 0.05, width = c(0, 2), title = "Width", legend = c("max", ".", ".", "min"), palette = topo.colors, # Associate with a colour scale y.intersp = 1.5 # Vertical space between labels (also moves title) ) SizeLegend( "bottomleft", horiz = TRUE, width = c(4, 1), legend = c("Thick", "Thin"), palette = "darkred", inset = 0.06 # Make space for the bar. # A future release may calculate this automatically )