Skip to content

Commit

Permalink
Merge pull request #73 from jfisher-usgs/master
Browse files Browse the repository at this point in the history
Fix names when alpha applied
  • Loading branch information
jfisher-usgs authored Jul 14, 2018
2 parents c83f78b + 105fb64 commit b0410f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
20 changes: 12 additions & 8 deletions R/GetTolColors.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#' Tol Color Palettes
#'
#' This function creates a vector of \code{n} contiguous colors from
#' color schemes by Paul Tol (2018).
#' This function creates a vector of \code{n} colors from
#' qualitative and sequential color schemes by Paul Tol (2018).
#'
#' @param n 'integer'.
#' Number of colors to be in the palette, the maximum value is based on the specified color scheme.
#' Number of colors to be in the palette, the maximum value is
#' dependent on the specified color scheme.
#' @param scheme 'character'.
#' Color scheme: select
#' \code{"bright"}, \code{"vibrant"}, \code{"muted"}, or \code{"light"} for sequential colors; and
#' \code{"rainbow"} for discrete colors.
#' Color scheme: select \code{"bright"}, \code{"vibrant"},
#' \code{"muted"}, or \code{"light"} for qualitative colors; and
#' \code{"rainbow"} for sequential colors.
#' Where \code{n < 8} for \code{"bright"} and \code{"vibrant"},
#' \code{n < 10} for \code{"muted"} and \code{"light"}, and
#' \code{n < 24} for \code{"rainbow"}.
Expand Down Expand Up @@ -41,6 +42,8 @@
#' GetTolColors(23, scheme = "rainbow", plot = TRUE)
#' par(op)
#'
#' GetTolColors(10, scheme = "rainbow", alpha = 0.5, plot = TRUE)
#'

GetTolColors <- function(n, scheme=c("bright", "vibrant", "muted", "light", "rainbow"),
alpha=NULL, plot=FALSE) {
Expand Down Expand Up @@ -143,18 +146,19 @@ GetTolColors <- function(n, scheme=c("bright", "vibrant", "muted", "light", "rai
}

col <- if (scheme == "rainbow") pal[idx[[n]]] else pal[1:n]
labels <- names(col)

if (!is.null(alpha)) {
col <- grDevices::adjustcolor(col, alpha.f=alpha)
names(col) <- names(pal)[1:n]
names(col) <- labels
}

if (plot) {
graphics::plot.default(0, 0, type="n", xlim=c(0, 1), ylim=c(0, 1),
axes=FALSE, xlab="", ylab="", main=scheme)
graphics::rect(0:(n - 1) / n, 0, 1:n / n, 1, col=col, border="#D3D3D3")
at <- 0:(n - 1) / n + (1 / (2 * n))
graphics::axis(1, at=at, labels=names(col), tick=FALSE)
graphics::axis(1, at=at, labels=labels, tick=FALSE)
}

return(col)
Expand Down
15 changes: 9 additions & 6 deletions man/GetTolColors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0410f9

Please sign in to comment.