setFactorGraphics {easylegend}R Documentation

Prepare a graphics and legend (col, pch, lty) from categorical data.

Description

Prepare a graphics and legend (col, pch, lty) from categorical data.

Usage

setFactorGraphics(x, ...)

## Default S3 method:
setFactorGraphics(x, col = FALSE, pch = FALSE,
  fill = FALSE, leg = NULL, ord = TRUE, decreasing = FALSE,
  naCol = "black", naPch = 25, naFill = "black", naLeg = "na", ...)

## S3 method for class 'matrix'
setFactorGraphics(x, ...)

## S3 method for class 'RasterLayer'
setFactorGraphics(x, ...)

Arguments

x

A vector of numerical, character, boolean or factor values, representing categories (non-continuous, with a limited number of unique values).

col

Logical value. If TRUE, a color will be attributed to each category in x, and the legend prepared for lines or symbols colors. Alternatively, can also be a vector of character strings representing R colors (see 'Color Specification' in par), with one value per category in x. Should be in the same order as values in unique(x).

pch

Logical value. If TRUE, a symbol will be attributed to each category in x. Alternatively, can also be a vector of point symbols, with one value per category in x. Should be in the same order as values in unique(x).

fill

Logical value. If TRUE, a color will be attributed to each category in x, and the legend prepared for filled colors. Alternatively, can also be a vector of character strings representing R colors (see 'Color Specification' in par), with one value per category in x. Should be in the same order as values in unique(x).

leg

A vector of character strings with the label of each category in the legend. If NULL, legend's legends will be the same as as.character(values).

ord

Logical value. If TRUE, the legend is ordered (sorted).

decreasing

Logical value. If TRUE (not default), and ord = TRUE the legend is ordered (sorted) in decreasing order (i.e. low values on the bottom of the color scale and high values on the top of the color scale).

naCol

Single character string. Color for NA (missing) values.

naPch

Single character string. Symbol for NA (missing) values.

naFill

Single character string. Fill-color for NA (missing) values.

naLeg

Single character string. Legend (label) for NA (missing) values.

...

Additional parameters passed to specific methods.

Value

Returns a list of 3 function: col, a function that converts x-like values into colors; pch, a function that converts x-like values into symbols; legend, a function to draw a legend on a plot with the correct colors and symbols. That function accepts ... arguments, passed to the original legend function.

Examples

# GENERATE DUMMY DATASET X-Y-Z OF CORRELATED VARIABLES + GROUPS
# =============================================================

#  Base variable (x) and groups (g)
n <- 200 
x <- rnorm( n ) 
g <- sample( x = c("a","b"), size = n, replace = TRUE ) 

#   y is a function of x and group and random noise
y <- 2*x + rnorm( n ) 
y[ g == "a" ] <- y[ g == "a" ] + 1
y[ g == "b" ] <- y[ g == "b" ] + 2

#   z is a function of x and random noise
z <- 3*x + rnorm( n ) 


#   Bind into a data.frame
xyz <- data.frame( "x" = x, "y" = y, "z" = z, "g" = g, 
    stringsAsFactors = FALSE )
rm( x, y, z, g )



# CATEGORY OVERLAY ON A X-Y PLOT (POINTS) AND REGRESSION LINE
# ===========================================================

library( "easylegend" ) 

#   'Calibrate' the legend
fg <- setFactorGraphics( x = xyz[, "g" ], col = TRUE, pch = TRUE )

#   Plot x y and g
plot( x = xyz[, "x" ], y = xyz[, "y" ], col = fg$col( xyz[, "g" ] ), 
    pch = fg$pch( xyz[, "g" ] ), panel.first = grid() )

#   Add regression lines
for( gi in unique( xyz[, "g" ] ) ){ 
    abline( lm( y ~ x, data = subset( xyz, g == gi ) ), col = fg$col( gi ) ) 
}   

#   Add legend
fg$legend( x = "bottomright", title = "Group:", bty = "n" )
fg$legend( x = "topleft", pch = NULL, lwd = 1, title = "Regression:", bty = "n" )

plot of chunk unnamed-chunk-1

rm( fg )



# SAME, with MISSING GROUP-VALUES
# ===============================

xyz[ sample(x=1:n,size=round(n/5)), "g" ] <- NA

#   'Calibrate' the legend
fg <- setFactorGraphics( x = xyz[, "g" ], col = TRUE, pch = TRUE )

#   Plot x y and g
plot( x = xyz[, "x" ], y = xyz[, "y" ], col = fg$col( xyz[, "g" ] ), 
    pch = fg$pch( xyz[, "g" ] ), panel.first = grid() )

#   Add regression lines
abline( lm( y ~ x, data = subset( xyz, g == "a" )   ), col = fg$col( "a" ) ) 
abline( lm( y ~ x, data = subset( xyz, g == "b" )   ), col = fg$col( "b" ) ) 
abline( lm( y ~ x, data = subset( xyz, is.na( g ) ) ), col = fg$col( NA ) ) 

fg$legend( x = "bottomright", title = "Group:", bty = "n", lwd = 1 )

plot of chunk unnamed-chunk-1

rm( fg )

[Package easylegend version 1.2.4 Index]