switch.par: Make a vector of graphic parameters based on data.

View source: R/switch.par.r

switch.parR Documentation

Make a vector of graphic parameters based on data.

Description

color.ramp is an alias of switch.par, which having different default value for pal and may enhance readability in some cases.

Usage

switch.par(x, pal = function(n, ...) 1:n, ..., unique.pal = FALSE)

## Default S3 method:
switch.par(x, pal = function(n, ...) 1:n, ..., unique.pal = FALSE)

## S3 method for class 'numeric'
switch.par(
  x,
  pal = gg.colors,
  n.class = NULL,
  method = "quantile",
  ...,
  unique.pal = FALSE
)

Arguments

x

a vector of character or factor. Based on this vector, the vector of graphic parameteres is generated. Making colors for numeric mode of x is experimentally supported.

pal

a function or vector. If a function is specified for pal, this function assumes that the function accepts number of params for the first argument (n) and produces a vector of graphic parameter. e.g. gg.colors, rainbow, terrain.colors, and topo.colors can be used. If a vector of length 1 is specified for pal, this function produces vector of graphic parameters consists of same values. If a vector of length equal to the number of unique values of x is specified, this function assigns each value of grpahic parameter for each value of x.

unique.pal

a logical determines return value of the function.

n.class

number of groups. If NULL, n.class become number of unique values in x.

method

method of grouping. Currently, only "quantile" is supported.

Value

If unique.pal is TRUE, this function returns unique named vector of graphic parameters. For this case, this function returns non-named vector of graphic parameter with length 1 if x is NULL. If unique.pal is FALSE, this function returns vector of graphic parameters with length equal to the length of x. For this case, the vector has "palette" attribute which contains named vector of graphic parameters used for the result.

Methods (by class)

  • switch.par(default): default S3 method.

  • switch.par(numeric): method for numeric.

Examples

#---------------------------------------------------------------------------
# Example 1: set point color and character based on a character vector.
#---------------------------------------------------------------------------
data(iris)
plot(
    Sepal.Length ~ Petal.Length, data = iris,
    col = color.ramp(Species),
    pch = switch.par(Species)
)
# Add a legend
col <- color.ramp(iris$Species, unique.pal = TRUE)
pch <- switch.par(iris$Species, unique.pal = TRUE)
legend("topleft", legend = names(col), col = col, pch = pch, cex = 1)


#---------------------------------------------------------------------------
# Example 2: using different color function.
#---------------------------------------------------------------------------
plot(
    Sepal.Length ~ Petal.Length, data = iris, pch = 16,
    col = color.ramp(Species, pal = rainbow)
)
col <- color.ramp(iris$Species, pal = rainbow, unique.pal = TRUE)
legend("topleft", legend = names(col), col = col, pch = 16, cex = 1)


#---------------------------------------------------------------------------
# Examle 3: using named color palette.
#---------------------------------------------------------------------------
pal = c(setosa = "blue", versicolor = "red", virginica = "black")
plot(
    Sepal.Length ~ Petal.Length, data = iris, pch = 16,
    col = color.ramp(Species, pal = pal)
)
col <- color.ramp(iris$Species, pal = pal, unique.pal = TRUE)
legend("topleft", legend = names(col), col = col, pch = 16, cex = 1)


#---------------------------------------------------------------------------
# Example 4: using data.frame.
#---------------------------------------------------------------------------
iris2 <- iris
iris2$new.factor <- as.factor(letters[1:2])
plot(
    Sepal.Length ~ Petal.Length, data = iris2, pch = 16,
    col = color.ramp(iris2)
)
col <- color.ramp(iris2, unique.pal = TRUE)
legend("topleft", legend = names(col), col = col, pch = 16, cex = 1)

Marchen/partial.plot documentation built on Feb. 13, 2025, 4:18 a.m.