densityLegend: Create a colored density legend for visually representing the...

Description Usage Arguments Value Author(s) Examples

View source: R/densityLegend.R

Description

Create a colored density legend for visually representing the distribution of a color variable on a plot

Usage

1
densityLegend(x, colorPalette, colorBreaks, side = "right", main = NULL)

Arguments

x

a numeric vector used to create the density trace

colorPalette

a vector of color values

colorBreaks

a vector of cutoff values for the color regions

side

the side of the plot to place the desntiy legend

main

the main title for the density legend (optional, recommended to use a title that describes x

Value

none, graphics are added to the current device

Author(s)

Jason Waddell

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
library(ggplot2)
library(RColorBrewer)

# subset the data object
dsub <- subset(diamonds, x > 5 & x < 6 & y > 5 & y < 6)
dsub <- dsub[-which(dsub$z > 4), ]
dsub <- dsub[-which(dsub$z < 3), ]

# define color pallette, color vector and color region breaks
colorPalette <- brewer.pal(9, "Blues")[4:9]
colorObj <- splitColorVar(colorVar = dsub$z, colorPalette)
colorVec <- colorObj$colorVec
breaks <- colorObj$breaks

# plot the data
prepLegend(side = "right", proportion = 0.3)
oaTemplate(xlim = range(dsub$x), ylim = range(dsub$y),
		main = "Diamond Length by Width \n Colored by Depth",
		xlab = "Length (mm)", ylab = "Width (mm)")
points(x = dsub$x, y = dsub$y, col = colorVec, pch = 19, cex = 0.6)

# add the legend
densityLegend(x = dsub$z, colorPalette = colorPalette, side = "right",
		main = "Diamond Depth", colorBreaks = breaks)

Example output

Loading required package: oaColors

oaPlots documentation built on May 2, 2019, 6:46 a.m.