Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Norad colors as default, tidy some of thecode #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Description: Provides Norads colour palette and theme for creating ggplot2 graph
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Imports:
curl,
ggplot2,
Expand Down
16 changes: 13 additions & 3 deletions R/ggnorad.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#' Wrapper function to load norad theme
#' Wrapper function to load Norad theme for ggplot2.
#' Also sets Norad's discrete and continuous color palettes as the default for the session.
#'
#'@param style Plot style. One of "default", "biglight1", "biglight2", "bigdark".
#'@param style Plot style. One of "default", "biglight1", "biglight2", "bigdark",
#' "maplight", "mapdark", or "light"
#'@param norad_colors Logical. Should Norad's color palettes be set as the default for the session? Default is TRUE.
#'@export
#'@name ggnorad

ggnorad <- function(style = "default") {
ggnorad <- function(style = "default", norad_colors = TRUE) {

showtext::showtext_auto()

Expand Down Expand Up @@ -51,4 +54,11 @@ ggnorad <- function(style = "default") {
} else if (style == "mapdark") {
ggplot2::theme_set(theme_norad_map_dark())
}

if (norad_colors == TRUE) {
options(ggplot2.discrete.colour = scale_color_norad)
options(ggplot2.discrete.fill = scale_fill_norad)
options(ggplot2.continuous.colour = scale_color_norad_c)
options(ggplot2.continuous.fill = scale_fill_norad_c)
}
}
56 changes: 20 additions & 36 deletions R/palette_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,14 @@ norad_pal <- function(palette = "main", ...) {
#'

palette_gen <- function(palette = "main", direction = 1) {

function(n) {

if (n > length(norad_pal(palette)))
warning("Not enough colors in this palette!")

else {

all_colors <- norad_pal(palette)

all_colors <- unname(unlist(all_colors))

all_colors <- if (direction >= 0) all_colors else rev(all_colors)

color_list <- all_colors[1:n]

}
}
}
Expand All @@ -121,17 +113,15 @@ palette_gen <- function(palette = "main", direction = 1) {
#' @param ... Additional arguments passed to ggplot2::colorRampPalette()
#'

palette_gen_c <- function(palette = "main", direction = 1, ...) {

palette_gen_c <- function(palette = "greens", direction = 1, ...) {
if (!palette %in% c("greens", "browns", "purples")) {
stop("Only 'greens', 'browns', and 'purples' palettes are supported")
}
pal <- norad_pal(palette)

pal <- if (direction >= 0) pal else rev(pal)

grDevices::colorRampPalette(pal, ...)

}


#' Discrete color scale constructor for Norad colors
#'
#' @param palette Character name of palette in norad_palettes
Expand All @@ -142,8 +132,8 @@ palette_gen_c <- function(palette = "main", direction = 1, ...) {

scale_color_norad <- function(palette = "main", direction = 1, ...) {
ggplot2::discrete_scale(
"color", "norad",
palette_gen(palette, direction),
aesthetics = "color",
palette = palette_gen(palette, direction),
...
)
}
Expand All @@ -159,76 +149,70 @@ scale_color_norad <- function(palette = "main", direction = 1, ...) {

scale_fill_norad <- function(palette = "main", direction = 1, ...) {
ggplot2::discrete_scale(
"fill", "norad",
palette_gen(palette, direction),
aesthetics = "fill",
palette = palette_gen(palette, direction),
...
)
}

#' Continious color scale constructor for Norad colors
#'
#' @param palette Character name of palette in norad_palettes
#' @param palette Character name of palette in norad_palettes. One of "greens", "browns", or "purples"
#' @param direction Integer indicating whether the palette should be reversed
#' @param ... Additional arguments passed to ggplot2::scale_fill_gradientn()
#' @export
#'

scale_color_norad_c <- function(palette = "greens", direction = 1, ...) {

pal <- palette_gen_c(palette = palette, direction = direction)

pal <- palette_gen_c(
palette = palette,
direction = direction
)
ggplot2::scale_color_gradientn(colors = pal(256), ...)

}

#' Continuous fill scale constructor for Norad colors
#'
#' @param palette Character name of palette in norad_palettes
#' @param palette Character name of palette in norad_palettes. One of "greens", "browns", or "purples"
#' @param direction Integer indicating whether the palette should be reversed
#' @param ... Additional arguments passed to ggplot2::scale_fill_gradientn()
#' @export
#'

scale_fill_norad_c <- function(palette = "greens", direction = 1, ...) {

pal <- palette_gen_c(palette = palette, direction = direction)

pal <- palette_gen_c(
palette = palette,
direction = direction
)
ggplot2::scale_fill_gradientn(colors = pal(256), ...)

}

#' Binned fill scale constructor for Norad colors
#'
#' @param palette Character name of palette in norad_palettes
#' @param palette Character name of palette in norad_palettes. One of "greens", "browns", or "purples"
#' @param direction Integer indicating whether the palette should be reversed
#' @param breaks How to break down continuous data into bins. Computed automatically by default.
#' @param ... Additional arguments passed to ggplot2::scale_fill_steps()
#' @export
#'

scale_fill_norad_binned <- function(palette = "greens", direction = 1, breaks = waiver(), ...) {

pal <- norad_pal(palette)

ggplot2::scale_fill_steps(low = pal[1], high = pal[2], ...)

}

#' Binned color scale constructor for Norad colors
#'
#' @param palette Character name of palette in norad_palettes
#' @param palette Character name of palette in norad_palettes. One of "greens", "browns", or "purples"
#' @param direction Integer indicating whether the palette should be reversed
#' @param breaks How to break down continuous data into bins. Computed automatically by default.
#' @param ... Additional arguments passed to ggplot2::scale_color_steps()
#' @export
#'

scale_color_norad_binned <- function(palette = "greens", direction = 1, breaks = waiver(), ...) {

pal <- norad_pal(palette)

ggplot2::scale_color_steps(low = pal[1], high = pal[2], ...)

}

#' @examples
Expand Down
13 changes: 9 additions & 4 deletions man/ggnorad.Rd

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

2 changes: 1 addition & 1 deletion man/palette_gen_c.Rd

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

2 changes: 1 addition & 1 deletion man/scale_color_norad_binned.Rd

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

2 changes: 1 addition & 1 deletion man/scale_color_norad_c.Rd

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

2 changes: 1 addition & 1 deletion man/scale_fill_norad_binned.Rd

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

2 changes: 1 addition & 1 deletion man/scale_fill_norad_c.Rd

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