Skip to content

Commit

Permalink
adds ggplot2 scales and fills
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmackintosh committed Mar 26, 2021
1 parent 8fbccee commit c16f5e3
Show file tree
Hide file tree
Showing 39 changed files with 2,728 additions and 34 deletions.
54 changes: 54 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
# Generated by roxygen2: do not edit by hand

S3method(print,palette)
export(aqua_pal)
export(bangles_pal)
export(beck_pal)
export(boo_pal)
export(bwitched_pal)
export(deeelite_pal)
export(hole_pal)
export(nodoubt_pal)
export(pop_palette)
export(rickroll_pal)
export(scale_color_aqua)
export(scale_color_bangles)
export(scale_color_beck)
export(scale_color_boo)
export(scale_color_bwitched)
export(scale_color_deeelite)
export(scale_color_hole)
export(scale_color_nodoubt)
export(scale_color_rickroll)
export(scale_color_sclub)
export(scale_color_spice)
export(scale_color_steps_h)
export(scale_colour_aqua)
export(scale_colour_bangles)
export(scale_colour_beck)
export(scale_colour_boo)
export(scale_colour_bwitched)
export(scale_colour_deeelite)
export(scale_colour_hole)
export(scale_colour_nodoubt)
export(scale_colour_rickroll)
export(scale_colour_sclub)
export(scale_colour_spice)
export(scale_colour_steps_h)
export(scale_fill_aqua)
export(scale_fill_bangles)
export(scale_fill_beck)
export(scale_fill_boo)
export(scale_fill_bwitched)
export(scale_fill_deeelite)
export(scale_fill_hole)
export(scale_fill_nodoubt)
export(scale_fill_rickroll)
export(scale_fill_sclub)
export(scale_fill_spice)
export(scale_fill_steps_h)
export(sclub_pal)
export(spice_pal)
export(steps_h_pal)
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))
}
}
105 changes: 105 additions & 0 deletions R/bangles_palette.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
bangles_palette <- c("#6D3A34", "#825137", "#A47850", "#AD9E70", "#9DA49D",
"#7C708E", "#7E5876", "#7C5366", "#5F383F"
)

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

bangles_pal <- function(n, type = c("discrete", "continuous"),
reverse = FALSE){
bangles <- bangles_palette

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

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

type <- match.arg(type)

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

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

bangles <- scales::manual_pal(bangles)

return(bangles)
}

#' @title scale_color_bangles
#' @rdname bangles_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_bangles()
#' @importFrom ggplot2 discrete_scale scale_color_gradientn

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

#' @title scale_colour_bangles
#' @rdname bangles_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_bangles()
#' @importFrom ggplot2 discrete_scale scale_color_gradientn

scale_colour_bangles <- scale_color_bangles

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

scale_fill_bangles <- function(n, type = "discrete",
reverse = FALSE, ...){
if (type == "discrete") {
ggplot2::discrete_scale("fill", "bangles",
bangles_pal(n = n, type = type,
reverse = reverse), ...)
} else {
ggplot2::scale_fill_gradientn(colors = bangles_pal(n = n, type = type,
reverse = reverse)(256))
}
}
105 changes: 105 additions & 0 deletions R/beck_palette.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
beck_palette <- c("#8A4736", "#C1CE7D", "#87E04D", "#92DD8E",
"#9BC3D7", "#A67FC3", "#B03387", "#BA2874","#A53252"
)

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

beck_pal <- function(n, type = c("discrete", "continuous"),
reverse = FALSE){
beck <- beck_palette

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

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

type <- match.arg(type)

if (type == "discrete" && n > length(beck)) {
stop(glue::glue("I'm a loser baby. Palette does not have {n} colors, maximum is {length(beck)}!"))
}

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

beck <- scales::manual_pal(beck)

return(beck)
}

#' @title scale_color_beck
#' @rdname beck_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_beck()
#' @importFrom ggplot2 discrete_scale scale_color_gradientn

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

#' @title scale_colour_beck
#' @rdname beck_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_beck()
#' @importFrom ggplot2 discrete_scale scale_color_gradientn

scale_colour_beck <- scale_color_beck

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

scale_fill_beck <- function(n, type = "discrete",
reverse = FALSE, ...){
if (type == "discrete") {
ggplot2::discrete_scale("fill", "beck",
beck_pal(n = n, type = type,
reverse = reverse), ...)
} else {
ggplot2::scale_fill_gradientn(colors = beck_pal(n = n, type = type,
reverse = reverse)(256))
}
}
Loading

0 comments on commit c16f5e3

Please sign in to comment.