gradientLegend: Add a gradient legend to a plot.

Description Usage Arguments Author(s) See Also Examples

View source: R/plot.R

Description

Add a gradient legend to a contour plot (or other plot) to indicate the range of values represented by the color palette.

Usage

1
2
3
4
gradientLegend(valRange, color = "topo", nCol = 30, pos = 0.5, side = 4,
  length = 0.25, depth = 0.05, inside = TRUE, coords = FALSE,
  pos.num = NULL, n.seg = 3, border.col = "black", dec = NULL,
  fit.margin = TRUE)

Arguments

valRange

Range of the values that is represented by the color palette. Normally two value-vector. If a larger vector is provided, only the min and max values are being used.

color

Name of color palette to use ('topo', 'terrain', 'heat', 'rainbow'). Custom color palettes can also be provided, but then the argument nCol is ignored.

nCol

Number of colors in the color palette.

pos

A number indicating the position on the axis in proportion. Using the arguments length and depth and side the position of the legend is calculated automatically. Alternatively, one could provide a vector with 4 numbers, providing the xleft, ybottom, xright, ytop of a rectangle. These 4 points are indicated in proportions of the x- and y-axis. However, if the argument coords is set to TRUE, these positions are taken as values in the Cartesian coordinate system of the plot. Note: coords is only considered for 4-number vectors of pos.

side

Which axis to choose: 1=bottom, 2=left, 3=top, 4=right. Default is 4.

length

Number, indicating the width of the legend as proportion with respect to the axis indicated by side. Note: when pos is defined by 4 numbers, length is ignored.

depth

Number, indicating the height of the legend as proportion with respect to the axis perpendicular to side. Note: when pos is defined by 4 numbers, depth is ignored.

inside

Logical: whether or not to plot the legend inside or outside the plot area. Note: when pos is defined by 4 numbers, inside is ignored.

coords

Logical: whether or not pos is defined as coordinates. When FALSE, the default, pos is defined in proportions. Note: when pos is defined by 1 number, inside is ignored. #' @param color Name of color palette to use ('topo', 'terrain', 'heat', 'rainbow'). Custom color palettes can also be provided, but then the argument nCol is ignored.

pos.num

Numeric value, indicating the position of the numbers with respect to the tick marks. 1=bottom, 2=left, 3=top, 4=right.

n.seg

Number of ticks and markers on the scale.

border.col

Color of the border and the ticks.

dec

Number of decimals for rounding the numbers, set to NULL on default (no rounding).

fit.margin

Logical: whether the labels of the gradient legend should be forced to fit in the margin or not.

Author(s)

Jacolien van Rij

See Also

Other Functions for plotting: addInterval, add_bars, add_n_points, alphaPalette, alpha, check_normaldist, color_contour, dotplot_error, drawDevArrows, emptyPlot, errorBars, fill_area, getCoords, getFigCoords, getProps, legend_margin, marginDensityPlot, plot_error, plot_image, plotsurface, sortBoxplot

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# empty plot:
emptyPlot(1,1, main="Test plot")
gradientLegend(valRange=c(-14,14),pos=.5, side=3)

# This produces a warning, as there is no space for labels here:
## Not run: 
gradientLegend(valRange=c(-14,14),pos=.125, side=4, inside=FALSE)

## End(Not run)
# Following options to fix this:
## a. put labels on other side of legend -
## not a good option, as the labels will overlap with the plot
gradientLegend(valRange=c(-14,14),pos=.125, side=4, inside=FALSE, pos.num=2)
## b. put the legend in the plot region
emptyPlot(1,1, main="Test plot")
gradientLegend(valRange=c(-14,14),pos=.125, side=4, inside=TRUE)
## c. Increase the margins:
oldmar = par()$mar
par(mar=oldmar+c(0,0,0,1))
emptyPlot(1,1, main="Test plot")
gradientLegend(valRange=c(-14,14),pos=.125, side=4, inside=FALSE)
par(mar=oldmar)
## d. the last option is not a fix, but avoid warnings: 
## set fit.margin to FALSE
emptyPlot(1,1, main="Test plot")
gradientLegend(valRange=c(-14,14),pos=.125, side=4, inside=FALSE, fit.margin=FALSE)

# change border color (and font color too!)
gradientLegend(valRange=c(-14,14),pos=.75, length=.5,
color=alphaPalette('white', f.seq=seq(0,1, by=.1)), border.col=alpha('gray'))

# when defining custom points, it is still important to specify side:

gradientLegend(valRange=c(-14,14), pos=c(.5,.25,.7,-.05), coords=TRUE, 
border.col='red', side=1)

plotfunctions documentation built on Aug. 31, 2017, 1:02 a.m.