switch.par | R Documentation |
color.ramp
is an alias of switch.par
, which having different
default value for pal
and may enhance readability in some cases.
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
)
x |
a vector of character or factor.
Based on this vector, the vector of graphic parameteres is generated.
Making colors for numeric mode of |
pal |
a function or vector.
If a function is specified for |
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. |
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.
switch.par(default)
: default S3 method.
switch.par(numeric)
: method for numeric.
#---------------------------------------------------------------------------
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.