Skip to content

Commit

Permalink
Fix partial matches
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Jun 25, 2015
1 parent 517f550 commit 15ea715
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion R/colour-manip.r
Expand Up @@ -50,7 +50,7 @@ muted <- function(colour, l=30, c=70) col2hcl(colour, l=l, c=c)
#' @examples
#' alpha("red", 0.1)
#' alpha(colours(), 0.5)
#' alpha("red", seq(0, 1, length = 10))
#' alpha("red", seq(0, 1, length.out = 10))
alpha <- function(colour, alpha = NA) {
col <- col2rgb(colour, TRUE) / 255

Expand Down
4 changes: 2 additions & 2 deletions R/pal-brewer.r
Expand Up @@ -11,11 +11,11 @@
#' @examples
#' show_col(brewer_pal()(10))
#' show_col(brewer_pal("div")(5))
#' show_col(brewer_pal(pal = "Greens")(5))
#' show_col(brewer_pal(palette = "Greens")(5))
#'
#' # Can use with gradient_n to create a continous gradient
#' cols <- brewer_pal("div")(5)
#' show_col(gradient_n_pal(cols)(seq(0, 1, length = 30)))
#' show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30)))
brewer_pal <- function(type = "seq", palette = 1, direction = 1) {
pal <- pal_name(palette, type)

Expand Down
2 changes: 1 addition & 1 deletion R/pal-dichromat.r
Expand Up @@ -9,7 +9,7 @@
#'
#' # Can use with gradient_n to create a continous gradient
#' cols <- dichromat_pal("DarkRedtoBlue.12")(12)
#' show_col(gradient_n_pal(cols)(seq(0, 1, length = 30)))
#' show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30)))
dichromat_pal <- function(name) {
if (!any(name == names(dichromat::colorschemes))) {
stop("Palette name must be one of ",
Expand Down
32 changes: 16 additions & 16 deletions R/pal-gradient.r
@@ -1,25 +1,25 @@
#' Arbitrary colour gradient palette (continous).
#'
#'
#' @param colours vector of colours
#' @param values if colours should not be evenly positioned along the gradient
#' this vector gives the position (between 0 and 1) for each colour in the
#' \code{colours} vector. See \code{\link{rescale}} for a convience function
#' to map an arbitrary range to between 0 and 1.
#' @param space colour space in which to calculate gradient. "Lab" usually
#' best unless gradient goes through white.
#' best unless gradient goes through white.
#' @export
gradient_n_pal <- function(colours, values = NULL, space = "Lab") {
ramp <- colorRamp(colours, space = space)

function(x) {
if (length(x) == 0) return(character())

if (!is.null(values)) {
xs <- seq(0, 1, length = length(values))
xs <- seq(0, 1, length.out = length.out(values))
f <- approxfun(values, xs)
x <- f(x)
}

nice_rgb(ramp(x))
}
}
Expand All @@ -34,36 +34,36 @@ nice_rgb <- function(x) {
}

#' Diverging colour gradient (continous).
#'
#'
#' @param low colour for low end of gradient.
#' @param mid colour for mid point
#' @param high colour for high end of gradient.
#' @param space colour space in which to calculate gradient. "Lab" usually
#' best unless gradient goes through white.
#' best unless gradient goes through white.
#' @export
#' @examples
#' x <- seq(-1, 1, length = 100)
#' x <- seq(-1, 1, length.out = 100)
#' r <- sqrt(outer(x^2, x^2, "+"))
#' image(r, col = div_gradient_pal()(seq(0, 1, length = 12)))
#' image(r, col = div_gradient_pal()(seq(0, 1, length = 30)))
#' image(r, col = div_gradient_pal()(seq(0, 1, length = 100)))
#' image(r, col = div_gradient_pal()(seq(0, 1, length.out = 12)))
#' image(r, col = div_gradient_pal()(seq(0, 1, length.out = 30)))
#' image(r, col = div_gradient_pal()(seq(0, 1, length.out = 100)))
#'
#' library(munsell)
#' image(r, col = div_gradient_pal(low =
#' mnsl(complement("10R 4/6", fix = TRUE)))(seq(0, 1, length = 100)))
#' image(r, col = div_gradient_pal(low =
#' mnsl(complement("10R 4/6", fix = TRUE)))(seq(0, 1, length.out = 100)))
div_gradient_pal <- function(low = mnsl("10B 4/6"), mid = mnsl("N 8/0"), high = mnsl("10R 4/6"), space = "Lab") {
gradient_n_pal(c(low, mid, high), space = space)
}

#' Sequential colour gradient palette (continous).
#'
#'
#' @param low colour for low end of gradient.
#' @param high colour for high end of gradient.
#' @param space colour space in which to calculate gradient. "Lab" usually
#' best unless gradient goes through white.
#' best unless gradient goes through white.
#' @export
#' @examples
#' x <- seq(0, 1, length = 25)
#' x <- seq(0, 1, length.out = 25)
#' show_col(seq_gradient_pal()(x))
#' show_col(seq_gradient_pal("white", "black")(x))
#'
Expand Down
2 changes: 1 addition & 1 deletion R/pal-hue.r
Expand Up @@ -30,7 +30,7 @@ hue_pal <- function(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction
}

rotate <- function(x) (x + h.start) %% 360 * direction
hues <- rotate(seq(h[1], h[2], length = n))
hues <- rotate(seq(h[1], h[2], length.out = n))

grDevices::hcl(hues, c, l)
}
Expand Down
2 changes: 1 addition & 1 deletion R/scale-continuous.r
Expand Up @@ -23,7 +23,7 @@
cscale <- function(x, palette, na.value = NA_real_, trans = identity_trans()) {
stopifnot(is.trans(trans))

x <- trans$trans(x)
x <- trans$transform(x)
limits <- train_continuous(x)
map_continuous(palette, x, limits, na.value)
}
Expand Down
22 changes: 11 additions & 11 deletions R/trans-date.r
Expand Up @@ -4,8 +4,8 @@
#' @examples
#' years <- seq(as.Date("1910/1/1"), as.Date("1999/1/1"), "years")
#' t <- date_trans()
#' t$trans(years)
#' t$inv(t$trans(years))
#' t$transform(years)
#' t$inverse(t$transform(years))
#' t$format(t$breaks(range(years)))
date_trans <- function() {
trans_new("date", "from_date", "to_date", breaks = pretty_breaks())
Expand All @@ -28,44 +28,44 @@ from_date <- function(x) {
#' @examples
#' hours <- seq(ISOdate(2000,3,20, tz = ""), by = "hour", length.out = 10)
#' t <- time_trans()
#' t$trans(hours)
#' t$inv(t$trans(hours))
#' t$transform(hours)
#' t$inverse(t$transform(hours))
#' t$format(t$breaks(range(hours)))
time_trans <- function(tz = NULL) {

to_time <- function(x) {
force(x)
structure(x, class = c("POSIXt", "POSIXct"), tzone = tz)
}

from_time <- function(x) {
if (!inherits(x, "POSIXct")) {
stop("Invalid input: time_trans works with objects of class ",
stop("Invalid input: time_trans works with objects of class ",
"POSIXct only", call. = FALSE)
}
if (is.null(tz)) {
tz <<- attr(as.POSIXlt(x), "tzone")[[1]]
}
structure(as.numeric(x), names = names(x))
}

trans_new("time", "from_time", "to_time", breaks = pretty_breaks())
}


#' Regularly spaced dates.
#'
#'
#' @param width an interval specification, one of "sec", "min", "hour",
#' "day", "week", "month", "year". Can be by an integer and a space, or
#' followed by "s".
#' followed by "s".
#' @export
date_breaks <- function(width = "1 month") {
function(x) fullseq(x, width)
}


#' Formatted dates.
#'
#'
#' @param format Date format using standard POSIX specification. See
#' \code{\link{strptime}} for possible formats.
#' @param tz a time zone name, see \code{\link{timezones}}. Defaults
Expand Down
2 changes: 1 addition & 1 deletion R/trans.r
Expand Up @@ -56,5 +56,5 @@ as.trans <- function(x) {
#' @export
trans_range <- function(trans, x) {
trans <- as.trans(trans)
range(trans$trans(range(squish(x, trans$domain), na.rm = TRUE)))
range(trans$transform(range(squish(x, trans$domain), na.rm = TRUE)))
}
2 changes: 1 addition & 1 deletion man/alpha.Rd
Expand Up @@ -20,6 +20,6 @@ Vectorised in both colour and alpha.
\examples{
alpha("red", 0.1)
alpha(colours(), 0.5)
alpha("red", seq(0, 1, length = 10))
alpha("red", seq(0, 1, length.out = 10))
}

4 changes: 2 additions & 2 deletions man/brewer_pal.Rd
Expand Up @@ -22,11 +22,11 @@ Color Brewer palette (discrete).
\examples{
show_col(brewer_pal()(10))
show_col(brewer_pal("div")(5))
show_col(brewer_pal(pal = "Greens")(5))
show_col(brewer_pal(palette = "Greens")(5))

# Can use with gradient_n to create a continous gradient
cols <- brewer_pal("div")(5)
show_col(gradient_n_pal(cols)(seq(0, 1, length = 30)))
show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30)))
}
\references{
\url{http://colorbrewer2.org}
Expand Down
4 changes: 2 additions & 2 deletions man/date_trans.Rd
Expand Up @@ -12,8 +12,8 @@ Transformation for dates (class Date).
\examples{
years <- seq(as.Date("1910/1/1"), as.Date("1999/1/1"), "years")
t <- date_trans()
t$trans(years)
t$inv(t$trans(years))
t$transform(years)
t$inverse(t$transform(years))
t$format(t$breaks(range(years)))
}

2 changes: 1 addition & 1 deletion man/dichromat_pal.Rd
Expand Up @@ -19,6 +19,6 @@ show_col(dichromat_pal("BluetoOrange.10")(5))

# Can use with gradient_n to create a continous gradient
cols <- dichromat_pal("DarkRedtoBlue.12")(12)
show_col(gradient_n_pal(cols)(seq(0, 1, length = 30)))
show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30)))
}

10 changes: 5 additions & 5 deletions man/div_gradient_pal.Rd
Expand Up @@ -21,14 +21,14 @@ best unless gradient goes through white.}
Diverging colour gradient (continous).
}
\examples{
x <- seq(-1, 1, length = 100)
x <- seq(-1, 1, length.out = 100)
r <- sqrt(outer(x^2, x^2, "+"))
image(r, col = div_gradient_pal()(seq(0, 1, length = 12)))
image(r, col = div_gradient_pal()(seq(0, 1, length = 30)))
image(r, col = div_gradient_pal()(seq(0, 1, length = 100)))
image(r, col = div_gradient_pal()(seq(0, 1, length.out = 12)))
image(r, col = div_gradient_pal()(seq(0, 1, length.out = 30)))
image(r, col = div_gradient_pal()(seq(0, 1, length.out = 100)))

library(munsell)
image(r, col = div_gradient_pal(low =
mnsl(complement("10R 4/6", fix = TRUE)))(seq(0, 1, length = 100)))
mnsl(complement("10R 4/6", fix = TRUE)))(seq(0, 1, length.out = 100)))
}

2 changes: 1 addition & 1 deletion man/seq_gradient_pal.Rd
Expand Up @@ -19,7 +19,7 @@ best unless gradient goes through white.}
Sequential colour gradient palette (continous).
}
\examples{
x <- seq(0, 1, length = 25)
x <- seq(0, 1, length.out = 25)
show_col(seq_gradient_pal()(x))
show_col(seq_gradient_pal("white", "black")(x))

Expand Down
4 changes: 2 additions & 2 deletions man/time_trans.Rd
Expand Up @@ -16,8 +16,8 @@ Transformation for times (class POSIXt).
\examples{
hours <- seq(ISOdate(2000,3,20, tz = ""), by = "hour", length.out = 10)
t <- time_trans()
t$trans(hours)
t$inv(t$trans(hours))
t$transform(hours)
t$inverse(t$transform(hours))
t$format(t$breaks(range(hours)))
}

4 changes: 2 additions & 2 deletions tests/testthat/test-alpha.r
Expand Up @@ -7,7 +7,7 @@ hex <- function(x) {

test_that("missing alpha preserves existing", {
cols <- col2rgb(rep("red", 5), TRUE) / 255
cols[4, ] <- seq(0, 1, length = ncol(cols))
cols[4, ] <- seq(0, 1, length.out = ncol(cols))

reds <- rgb(cols[1,], cols[2,], cols[3,], cols[4, ])

Expand All @@ -24,7 +24,7 @@ test_that("alpha values recycled to match colour", {
})

test_that("col values recycled to match alpha", {
alphas <- round(seq(0, 255, length = 3))
alphas <- round(seq(0, 255, length.out = 3))
reds <- alpha("red", alphas / 255)
reds_alpha <- col2rgb(reds, TRUE)[4, ]

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-scale.r
@@ -1,7 +1,7 @@
context("Scale")

test_that("NA.value works for continuous scales", {
x <- c(NA, seq(0, 1, length = 10), NA)
x <- c(NA, seq(0, 1, length.out = 10), NA)
pal <- rescale_pal()

expect_that(cscale(x, pal)[1], equals(NA_real_))
Expand Down

0 comments on commit 15ea715

Please sign in to comment.