Skip to content

Commit

Permalink
retry
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmackintosh committed Mar 24, 2021
1 parent 46db03d commit cbc0cc7
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 21 deletions.
9 changes: 9 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Imports:
ggplot2,
scales,
glue
Suggests:
dplyr,
gapminder,
testthat,
covr
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# Generated by roxygen2: do not edit by hand

S3method(print,palette)
export(aqua_pal)
export(pop_palette)
export(print_pal)
export(scale_color_aqua)
export(scale_colour_aqua)
export(scale_fill_aqua)
importFrom(ggplot2,discrete_scale)
importFrom(ggplot2,scale_color_gradientn)
importFrom(ggplot2,scale_fill_gradientn)
importFrom(glue,glue)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,rgb)
importFrom(graphics,image)
importFrom(graphics,par)
importFrom(graphics,rect)
importFrom(graphics,text)
importFrom(scales,manual_pal)
106 changes: 106 additions & 0 deletions R/aqua_palette.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
aqua_palette <- c(
"#B46843", "#C0AB45", "#5F8841", "#32956F", "#31A6B3",
"#1EA8CF", "#2A638E", "#834583", "#BB3855"
)

#' @title aqua palette
#' @description aqua palette
#' @inheritDotParams ggplot2::discrete_scale
#' @param n number of colors
#' @param type discrete or continuous
#' @param reverse reverse order, Default: FALSE
#' @rdname aqua_pal
#' @examples
#' library(scales)
#' show_col(aqua_pal()(9))
#' @export
#' @importFrom scales manual_pal
#' @importFrom glue glue
#' @importFrom grDevices colorRampPalette

aqua_pal <- function(n, type = c("discrete", "continuous"),
reverse = FALSE){
aqua <- aqua_palette

if (reverse == TRUE) {
aqua <- rev(aqua)
}

if (missing(n)) {
n <- length(aqua)
}

type <- match.arg(type)

if (type == "discrete" && n > length(aqua)) {
stop(glue::glue("Palette does not have {n} colors, maximum is {length(aqua)}!"))
}

aqua <- switch(type,
continuous = grDevices::colorRampPalette(aqua)(n),
discrete = aqua[1:n])

aqua <- scales::manual_pal(aqua)

return(aqua)
}

#' @title scale_color_aqua
#' @rdname aqua_pal
#' @export
#' @examples
#'
#' library(ggplot2)
#' ggplot(airquality, aes(x = Day, y = Temp,
#' group = as.factor(Month), color = as.factor(Month))) +
#' geom_point(size = 2.5) +
#' scale_color_aqua()
#' @importFrom ggplot2 discrete_scale scale_color_gradientn

scale_color_aqua <- function(n, type = "discrete",
reverse = FALSE, ...){
if (type == "discrete") {
ggplot2::discrete_scale("color", "aqua",
aqua_pal(n = n, type = type,
reverse = reverse), ...)
} else {
ggplot2::scale_color_gradientn(colors = aqua_pal(n = n, type = type,
reverse = reverse)(256))
}
}

#' @title scale_colour_aqua
#' @rdname aqua_pal
#' @export
#' @examples
#'
#' ggplot(airquality, aes(x = Day, y = Temp,
#' group = as.factor(Month), color = as.factor(Month))) +
#' geom_point(size = 2.5) +
#' scale_colour_aqua()
#' @importFrom ggplot2 discrete_scale scale_color_gradientn

scale_colour_aqua <- scale_color_aqua

#' @title scale_fill_aqua
#' @rdname aqua_pal
#' @export
#' @examples
#'
#' ggplot(mpg, aes(displ)) +
#' geom_histogram(aes(fill = class),
#' col = "black", size = 0.1) +
#' scale_fill_aqua()
#' @importFrom ggplot2 discrete_scale scale_fill_gradientn

scale_fill_aqua <- function(n, type = "discrete",
reverse = FALSE, ...){
if (type == "discrete") {
ggplot2::discrete_scale("fill", "aqua",
aqua_pal(n = n, type = type,
reverse = reverse), ...)
} else {
ggplot2::scale_fill_gradientn(colors = aqua_pal(n = n, type = type,
reverse = reverse)(256))
}
}
23 changes: 15 additions & 8 deletions R/pop_palettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pop_palettes <- list(
"#9BC3D7", "#A67FC3", "#B03387", "#BA2874","#A53252"),
boo = c("#B04D49", "#C06A51", "#C89B68", "#A79F62" ,"#4D6D7B",
"#42496C", "#633D5A", "#934B63", "#A34351"),
# boo2 = c("#97807D", "#B69789", "C7B3A2", "#CEC9B6", "#98B9B7",
# "#6A97A6", "#6F7C89", "#544E58", "#A95E71"),
breeders = c("#792F28", "#763F25", "#856426", "#818425", "#799821",
"#3C7135", "#5F253B", "#89172A", "#93242A"),
bwitched = c("#B1473C", "#BB553E", "#C65B3F", "#C86141", "#D26D48",
Expand All @@ -25,14 +23,14 @@ pop_palettes <- list(
"#ABCEDF", "#BBC2E1", "#E0BFDF", "#CC798C"),
steps = c("#A65A57", "#D18C7E", "#F2BEA3", "#E5DFBD", "#85BACB",
"#8895C4", "#BB6CA6", "#BA638C", "#9B535A")

)


#' Color Palettes based on 90s pop album covers
#'
#' R package that contains color palettes based on 90s pop album covers.
#'

#' Color Palettes based on pop album covers
#'
#' R package that contains color palettes based on pop album covers.
#' See also: https://github.com/johnmackintosh/metallicaRt for metallica palettes
#'
#' and https://github.com/johnmackintosh/rockthemes for rock palettes
Expand All @@ -42,9 +40,9 @@ pop_palettes <- list(
#' \code{breeders}, \code{bwitched}, \code{deeelite},\code{hole},
#' \code{nodoubt}, \code{sclub7}, \code{spice},\code{steps}
#'
#'
#' @param n Number of colors desired.
#'
#'
#' @param type Either continuous or discrete.
#'
#' @return A vector of colors.
Expand Down Expand Up @@ -75,5 +73,14 @@ pop_palette <- function(name, n, type = c("discrete", "continuous")) {
structure(out, class = "palette", name = name)
}

#' @export
#' @importFrom graphics rect par image text
#' @importFrom grDevices rgb
print.palette <- function(x, ...) {
n <- length(x)
old <- par(mar = c(0.5, 0.5, 0.5, 0.5))
on.exit(par(old))


image(1:n, 1, as.matrix(1:n), col = x,
ylab = "", xaxt = "n", yaxt = "n", bty = "n")
}
107 changes: 107 additions & 0 deletions man/aqua_pal.Rd

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

7 changes: 3 additions & 4 deletions man/pop_palette.Rd

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

Loading

0 comments on commit cbc0cc7

Please sign in to comment.