Learn R Programming

oaPlots (version 0.0.25)

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

Description

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

Usage

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

Examples

Run this code
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)

Run the code above in your browser using DataLab