Skip to content

Commit

Permalink
Merge pull request #19 from noradno/dev
Browse files Browse the repository at this point in the history
Add monocolor gradient scales for greens, browns and purples. Tidy an…
  • Loading branch information
einartornes authored May 9, 2023
2 parents e99f8ba + ffcf8dd commit e276d95
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 41 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: noradplot
Type: Package
Title: Norad colour palette and theme for ggplot2 graphics
Version: 0.3.0
Version: 0.4.0
Authors@R: c(
person("Einar", "Tornes", email = "[email protected]", role = c("aut", "cre")),
person("Eivind", "Hammersmark", "Moe", email = "[email protected]", role = "aut"),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ export(norad_cols)
export(norad_gradient)
export(norad_pal)
export(scale_color_norad)
export(scale_color_norad_binned)
export(scale_color_norad_c)
export(scale_fill_norad)
export(scale_fill_norad_binned)
export(scale_fill_norad_c)
export(theme_norad)
export(theme_norad_big_dark)
Expand Down
2 changes: 1 addition & 1 deletion R/ggnorad.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Wrapper function to load norad theme
#'
#'@param ... Additional arguments
#'@param style Plot style. One of "default", "biglight1", "biglight2", "bigdark".
#'@export
#'@name ggnorad

Expand Down
76 changes: 58 additions & 18 deletions R/palette_functions.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#' Gradient color scale constructor for Norad colors
#'
#' @param n Number of colors
#' @param color color upon which to generate gradient.
#' @export


norad_gradient <- function(n) {
stopifnot("n must be greater than zero" = n > 0)
color_high <- "#1B3A1C"
norad_gradient <- function(n, color = norad_cols("green")) {
stopifnot("n must be greater than one" = n > 1)
color_high <- color
color_high_hsl <- plotwidgets::col2hsl(color_high)
color_low_hsl <- color_high_hsl
color_low_hsl[3] <- 1 - (1 - color_high_hsl[3])*0.1 # 90 % lighter
color_low_hsl[3] <- 1 - (1 - color_high_hsl[3]) * 0.1 # 90 % lighter
color_low <- plotwidgets::hsl2col(color_low_hsl)

if (n > 2) {
Expand Down Expand Up @@ -63,18 +64,23 @@ norad_cols <- function(...) {
#' Function to access Norad palettes
#'
#' @param palette Character name of palette in norad_palettes
#' @param ... Additional arguments
#' @param ... Additional arguments (not in use)
#' @export
#'

norad_pal <- function(palette = "main", ...) {

green_light <- "#D6FEEB" # 90 % lighter than green
brown_light <- "#FBE6DC"
blue_light <- "#D9DEFB"

norad_palettes <- list(
"main" = norad_cols(),
"green" = norad_cols("green"),
"greens" = norad_cols("lightgreen", "green"),
"greenyellow" = norad_cols("green", "yellow"),
"greenblue" = norad_cols("green", "lightblue"))
"main" = norad_cols(),
"green" = norad_cols("green"),
"greens" = c(green_light, norad_cols("green")),
"browns" = c(brown_light, norad_cols("brown")),
"purples" = c(blue_light, norad_cols("blue"))
)

norad_palettes[[palette]]

Expand Down Expand Up @@ -112,7 +118,7 @@ palette_gen <- function(palette = "main", direction = 1) {
#'
#' @param palette Character name of palette in norad_palettes
#' @param direction Integer indicating whether the palette should be reversed
#' @param ... Additional arguments passed to colorRampPalette()
#' @param ... Additional arguments passed to ggplot2::colorRampPalette()
#'

palette_gen_c <- function(palette = "main", direction = 1, ...) {
Expand All @@ -130,7 +136,7 @@ palette_gen_c <- function(palette = "main", direction = 1, ...) {
#'
#' @param palette Character name of palette in norad_palettes
#' @param direction Integer indicating whether the palette should be reversed
#' @param ... Additional arguments passed to discrete_scale()
#' @param ... Additional arguments passed to ggplot2::discrete_scale()
#' @export
#'

Expand All @@ -147,7 +153,7 @@ scale_color_norad <- function(palette = "main", direction = 1, ...) {
#'
#' @param palette Character name of palette in norad_palettes
#' @param direction Integer indicating whether the palette should be reversed
#' @param ... Additional arguments passed to discrete_scale()
#' @param ... Additional arguments passed to ggplot2::discrete_scale()
#' @export
#'

Expand All @@ -163,34 +169,68 @@ scale_fill_norad <- function(palette = "main", direction = 1, ...) {
#'
#' @param palette Character name of palette in norad_palettes
#' @param direction Integer indicating whether the palette should be reversed
#' @param ... Additional arguments passed to discrete_scale()
#' @param ... Additional arguments passed to ggplot2::scale_fill_gradientn()
#' @export
#'

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

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

ggplot2::scale_color_gradientn(colors = pal(256), ...)

}

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

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

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 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 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
#' ggplot(mtcars, aes(factor(cyl), mpg)) +
#' geom_bar(aes(fill = factor(cyl)), stat = "identity") +
Expand Down
18 changes: 9 additions & 9 deletions R/style_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ theme_norad_big_light_1 <- function() {
axis.line = ggplot2::element_line(color = norad_cols("green")),
axis.ticks = ggplot2::element_blank(),
# Facet labels
strip.text = element_text(size = 20, color = norad_cols("lightgreen")),
strip.background = element_rect(fill = norad_cols("green"), color = NA),
strip.text = ggplot2::element_text(size = 20, color = norad_cols("lightgreen")),
strip.background = ggplot2::element_rect(fill = norad_cols("green"), color = NA),
# Plot and panel background
plot.background = ggplot2::element_rect(fill = "#eff6ea", color = NA),
panel.background = ggplot2::element_blank(),
# Plot margin
plot.margin = margin(1, 0, 1, 1, "cm")
plot.margin = ggplot2::margin(1, 0, 1, 1, "cm")
)
}

Expand All @@ -127,15 +127,15 @@ theme_norad_big_dark <- function() {

theme_norad_big_light_1() +
ggplot2::theme(
text = element_text(color = norad_cols("lightgreen")),
text = ggplot2::element_text(color = norad_cols("lightgreen")),
plot.background = ggplot2::element_rect(fill = "#1b3a1c"),
panel.background = ggplot2::element_blank(),
plot.title = ggplot2::element_blank(),
plot.subtitle = ggplot2::element_blank(),
axis.text = ggplot2::element_text(size = 20, color = norad_cols("lightgreen")),
axis.line = ggplot2::element_line(color = norad_cols("lightgreen")),
strip.text = element_text(size = 20, color = norad_cols("green")),
strip.background = element_rect(fill = norad_cols("lightgreen"), color = NA)
strip.text = ggplot2::element_text(size = 20, color = norad_cols("green")),
strip.background = ggplot2::element_rect(fill = norad_cols("lightgreen"), color = NA)
)
}

Expand All @@ -150,15 +150,15 @@ theme_norad_big_light_2 <- function() {

theme_norad_big_light_1() +
ggplot2::theme(
text = element_text(color = norad_cols("blue")),
text = ggplot2::element_text(color = norad_cols("blue")),
plot.background = ggplot2::element_rect(fill = "#eee8e7"),
panel.background = ggplot2::element_blank(),
plot.title = ggplot2::element_blank(),
plot.subtitle = ggplot2::element_blank(),
axis.text = ggplot2::element_text(size = 20, color = norad_cols("blue")),
axis.line = ggplot2::element_line(color = norad_cols("blue")),
strip.text = element_text(size = 20, color = norad_cols("blue")),
strip.background = element_rect(fill = norad_cols("darkred"), color = NA)
strip.text = ggplot2::element_text(size = 20, color = norad_cols("blue")),
strip.background = ggplot2::element_rect(fill = norad_cols("darkred"), color = NA)
)
}

Expand Down
2 changes: 1 addition & 1 deletion man/ggnorad.Rd

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

4 changes: 3 additions & 1 deletion man/norad_gradient.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/norad_pal.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.Rd

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

25 changes: 25 additions & 0 deletions man/scale_color_norad_binned.Rd

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

4 changes: 2 additions & 2 deletions 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.Rd

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

25 changes: 25 additions & 0 deletions man/scale_fill_norad_binned.Rd

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

8 changes: 4 additions & 4 deletions man/scale_fill_norad_c.Rd

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

0 comments on commit e276d95

Please sign in to comment.