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 all commits
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
Binary file added Meta/vignette.rds
Binary file not shown.
18 changes: 15 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,13 @@ 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)
options(ggplot2.binned.fill = scale_fill_norad_binned)
options(ggplot2.binned.colour = scale_color_norad_binned)
}
}
58 changes: 22 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,9 @@ 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",
scale_name = "norad", # Deprecated in 3.5.0, but required for version <= 3.5.0, so keep for now
palette = palette_gen(palette, direction),
...
)
}
Expand All @@ -159,76 +150,71 @@ 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",
scale_name = "norad", # Deprecated in 3.5.0, but required for version <= 3.5.0, so keep for now
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
50 changes: 50 additions & 0 deletions doc/noradplot-no-subtitle.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
dev = "svglite"
)

## ----setup--------------------------------------------------------------------
library(ggplot2)
library(noradplot)

ggnorad("default")


## -----------------------------------------------------------------------------
ggnorad("default")

ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
scale_color_norad() +
labs(title = "Displacement and highway mileage",
caption = "Source: mpg dataset from ggplot2 package")

## -----------------------------------------------------------------------------
ggplot(mpg, aes(class)) +
geom_bar() +
scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
labs(title = "Number of vehicles",
caption = "Source: mpg dataset from ggplot2 package")

## ----message=FALSE------------------------------------------------------------
library(dplyr)

df <- tibble("temp" = as.numeric(nhtemp), "time" = as.numeric(time(nhtemp)))

df |>
ggplot(aes(time, temp)) +
geom_line() +
scale_color_norad() +
labs(title = "New Haven temperature",
caption = "Source: nhtemp dataset from datasets package")

## -----------------------------------------------------------------------------
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
scale_color_norad() +
labs(title = "Displacement and highway mileage",
caption = "Source: mpg dataset from ggplot2 package") +
facet_wrap(~class)

79 changes: 79 additions & 0 deletions doc/noradplot-no-subtitle.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: "noradplot"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{noradplot}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
dev = "svglite"
)
```

```{r setup}
library(ggplot2)
library(noradplot)

ggnorad("default")

```

## Scatter plot
```{r}
#| fig.width: 6
#| fig.height: 4
ggnorad("default")

ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
scale_color_norad() +
labs(title = "Displacement and highway mileage",
caption = "Source: mpg dataset from ggplot2 package")
```

## Bar chart
```{r}
#| fig.width: 6
#| fig.height: 4

ggplot(mpg, aes(class)) +
geom_bar() +
scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
labs(title = "Number of vehicles",
caption = "Source: mpg dataset from ggplot2 package")
```

## Line chart
```{r, message=FALSE}
#| fig.width: 6
#| fig.height: 4

library(dplyr)

df <- tibble("temp" = as.numeric(nhtemp), "time" = as.numeric(time(nhtemp)))

df |>
ggplot(aes(time, temp)) +
geom_line() +
scale_color_norad() +
labs(title = "New Haven temperature",
caption = "Source: nhtemp dataset from datasets package")
```

## Faceted plot
```{r}
#| fig.width: 6
#| fig.height: 5

ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
scale_color_norad() +
labs(title = "Displacement and highway mileage",
caption = "Source: mpg dataset from ggplot2 package") +
facet_wrap(~class)
```
Loading