Learn R Programming

mapmisc (version 1.1.1)

colourScale: Create colour scales

Description

Produces a scale of colours for plotting maps an producing legends.

Usage

colourScale(x, breaks=5, style=c("quantile","equal","unique", "fixed"),
	 col="YlOrRd", opacity=1, dec=NULL, firstBreak=NULL, 
	 transform=NULL, revCol=FALSE, exclude=NULL, labels=NULL,...)
legendBreaks(pos, breaks, outer=TRUE,...)

Arguments

x
A vector or single-layer Raster, numeric or factor, for which a colour scale will be created
breaks
For colourScale either the number of or vector of breaks. for legendBreaks usually the output of colourScale, or a vector of breaks
style
Style for breaks, see Details
col
Colours to use, either a function or argument for brewer.pal
opacity
adds transparency to colours, either a single number, vector of length 2, or vector of same length as breaks
dec
Number of decimal places for the breaks
firstBreak
If non-null, force the first break to take this value (often zero).
transform
A list of two functions to transform x and inverse transform the breaks, or a numeric value specifying a Box-Cox parameter.
revCol
Reverse the order of the colours.
exclude
A vector of values to change to NA when they appear in x
labels
Vector of names of levels, useful when style='unique'
pos
Position, as specified in the legend function.
outer
If TRUE, put legend in the margin of the plot
...
Additional arguments passed to classIntervals or legend.

Value

  • A list with elements
  • plotVector of same length of x containing colours (RGB hex)
  • breaksvector of break points
  • colvector of unique colour values corresponding to breaks
  • colWithOpacityas col, but with two digit transparency values appended.

Details

colourScale produces intervals from x, each with a unique colour. Categories are determined with break points according to the following style options:
  • quantile:quantile(x, prob=seq(0,1,len=breaks), )

equal: seq(min(x), max(x), len=breaks)

unique: sort(table(unique(x)))[1:breaks]

fixed: breaks

any other string: is passed to classIntervals

legendBreaks produces a legend for 'z-axis' colour scales.

See Also

scaleBar, classIntervals

Examples

Run this code
Npoints = 20
myPoints = SpatialPointsDataFrame(20*cbind(runif(Npoints), runif(Npoints)), 
	data=data.frame(y1=c(NA, rnorm(Npoints-1)), 
	y2=c(sample(1:4, Npoints-1,replace=TRUE), NA)),
	proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
	)

mymap = openmap(myPoints)

if(require('RColorBrewer', quietly=TRUE)) {
	theCol = 'RdYlBu'
} else {
	theCol = heat.colors
}

myscale = colourScale(myPoints$y1, breaks=4, col=theCol,
 style="quantile", revCol=TRUE,dec=1)



map.new(myPoints)
plot(mymap,add=TRUE)
plot(myPoints, col=myscale$plot, pch=16,add=TRUE)
legendBreaks("topleft", breaks=myscale)


myscale2 = colourScale(myPoints$y1, breaks=8, col=rainbow, style="equal",
opacity=0.8, dec=2, revCol=TRUE)

map.new(myPoints)
plot(mymap,add=TRUE)
plot(myPoints, col=myscale2$plot, pch=16,add=TRUE)
legendBreaks("topleft", breaks=myscale2)

if(require('RColorBrewer', quietly=TRUE)) {
	theCol = 'Set2'
} else {
	theCol = heat.colors
}

myscale3 = colourScale(myPoints$y2,  breaks=3,col=theCol, style="unique",
	opacity=c(0.1, 0.9))

map.new(myPoints)
plot(mymap,add=TRUE)
plot(myPoints, col=myscale3$plot, pch=16,add=TRUE)
legendBreaks("topleft", breaks=myscale3)

myPoints$y3 = exp(myPoints$y1)
myscale4 = colourScale(myPoints$y3,  breaks=4, style="equal",
	opacity=c(0.1, 0.9), transform=1.25,dec=0, firstBreak=0)

map.new(myPoints)
plot(mymap,add=TRUE)
plot(myPoints, col=myscale4$plot, pch=16,add=TRUE)
legendBreaks("topleft", breaks=myscale4$breaks, col=myscale4$col)

Run the code above in your browser using DataLab