Skip to content
Merged
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
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(pal_material)
export(pal_nejm)
export(pal_npg)
export(pal_observable)
export(pal_primer)
export(pal_rickandmorty)
export(pal_simpsons)
export(pal_startrek)
Expand Down Expand Up @@ -49,6 +50,7 @@ export(scale_color_material)
export(scale_color_nejm)
export(scale_color_npg)
export(scale_color_observable)
export(scale_color_primer)
export(scale_color_rickandmorty)
export(scale_color_simpsons)
export(scale_color_startrek)
Expand All @@ -74,6 +76,7 @@ export(scale_colour_material)
export(scale_colour_nejm)
export(scale_colour_npg)
export(scale_colour_observable)
export(scale_colour_primer)
export(scale_colour_rickandmorty)
export(scale_colour_simpsons)
export(scale_colour_startrek)
Expand All @@ -99,6 +102,7 @@ export(scale_fill_material)
export(scale_fill_nejm)
export(scale_fill_npg)
export(scale_fill_observable)
export(scale_fill_primer)
export(scale_fill_rickandmorty)
export(scale_fill_simpsons)
export(scale_fill_startrek)
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# ggsci 3.2.0.9000

## New features

- Add the Primer palette in `scale_color_primer()`
and `scale_fill_primer()` (#62).

## Improvements

- Add `example_scatterplot()` and `example_barplot()` to simplify
discrete scale examples in documentation. This reduces boilerplate code
and makes it easier to maintain the examples.
Also improves graphical appearance of the examples by using a minimalist
theme with alternative data subsets.
theme with alternative data subsets (#61).

# ggsci 3.2.0

Expand Down
87 changes: 87 additions & 0 deletions R/discrete-primer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#' Primer design system palette
#'
#' The Primer design system data visualization palette.
#'
#' @param palette Palette type.
#' Currently there is one available option: `"default"`
#' (17-color palette).
#' @param alpha Transparency level, a real number in (0, 1].
#' See `alpha` in [grDevices::rgb()] for details.
#'
#' @export pal_primer
#'
#' @importFrom grDevices col2rgb rgb
#' @importFrom scales manual_pal
#'
#' @author Nan Xiao | \email{me@nanx.me} | <https://nanx.me>
#'
#' @references
#' GitHub (2024). "Primer data visualization colors."
#' <https://primer.style/product/ui-patterns/data-visualization/>
#'
#' @examples
#' library("scales")
#' show_col(pal_primer("default")(17))
#' show_col(pal_primer("default", alpha = 0.6)(17))
pal_primer <- function(palette = c("default"), alpha = 1) {
palette <- match.arg(palette)

if (alpha > 1L || alpha <= 0L) stop("alpha must be in (0, 1]")

raw_cols <- ggsci_db$"primer"[[palette]]
raw_cols_rgb <- col2rgb(raw_cols)
alpha_cols <- rgb(
raw_cols_rgb[1L, ], raw_cols_rgb[2L, ], raw_cols_rgb[3L, ],
alpha = alpha * 255L, names = names(raw_cols),
maxColorValue = 255L
)

manual_pal(unname(alpha_cols))
}

#' Primer color scales
#'
#' See [pal_primer()] for details.
#'
#' @inheritParams pal_primer
#' @param ... Additional parameters for [ggplot2::discrete_scale()].
#'
#' @export scale_color_primer
#'
#' @importFrom ggplot2 discrete_scale
#'
#' @author Nan Xiao | \email{me@nanx.me} | <https://nanx.me>
#'
#' @references
#' GitHub (2024). "Primer data visualization colors."
#' <https://primer.style/product/ui-patterns/data-visualization/>
#'
#' @rdname scale_primer
#'
#' @examples
#' example_scatterplot() + scale_color_primer()
#' example_barplot() + scale_fill_primer()
scale_color_primer <- function(palette = c("default"), alpha = 1, ...) {
palette <- match.arg(palette)
if (is_ggplot2_350()) {
discrete_scale("colour", palette = pal_primer(palette, alpha), ...)
} else {
discrete_scale("colour", scale_name = "primer", palette = pal_primer(palette, alpha), ...)
}
}

#' @export scale_colour_primer
#' @rdname scale_primer
scale_colour_primer <- scale_color_primer

#' @export scale_fill_primer
#' @importFrom ggplot2 discrete_scale
#' @rdname scale_primer
scale_fill_primer <- function(palette = c("default"), alpha = 1, ...) {
palette <- match.arg(palette)
if (is_ggplot2_350()) {
discrete_scale("fill", palette = pal_primer(palette, alpha), ...)
} else {
discrete_scale("fill", scale_name = "primer", palette = pal_primer(palette, alpha), ...)
}
}
21 changes: 21 additions & 0 deletions R/palettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ ggsci_db$"observable"$"observable10" <- c(
"Gray" = "#9498A0"
)

# Primer data visualization color palette ----
ggsci_db$"primer"$"default" <- c(
"Blue" = "#006EDB",
"Orange" = "#EB670F",
"Red" = "#DF0C24",
"Teal" = "#179B9B",
"Green" = "#30A147",
"Purple" = "#894CEB",
"Yellow" = "#B88700",
"Pink" = "#CE2C85",
"Brown" = "#856D4C",
"Lime" = "#527A29",
"Coral" = "#D43511",
"Pine" = "#167E53",
"Auburn" = "#9D615C",
"Olive" = "#64762D",
"Plum" = "#A830E8",
"Lemon" = "#866E04",
"Gray" = "#808FA3"
)

# Color palette from IGV ----
ggsci_db$"igv"$"default" <- c(
"chr1" = "#5050FF", "chr2" = "#CE3D32", "chr3" = "#749B58",
Expand Down
8 changes: 8 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ p2_observable <- p2 + scale_fill_observable()
grid.arrange(p1_observable, p2_observable, ncol = 2)
```

### Primer

```{r, ggsci-primer}
p1_primer <- p1 + scale_color_primer()
p2_primer <- p2 + scale_fill_primer()
grid.arrange(p1_primer, p2_primer, ncol = 2)
```

### LocusZoom

```{r, ggsci-locuszoom}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ open with `vignette("ggsci")` in R) for a quick-start guide.

<img src="man/figures/README-ggsci-observable-1.png" width="100%" style="display: block; margin: auto;" />

### Primer

<img src="man/figures/README-ggsci-primer-1.png" width="100%" style="display: block; margin: auto;" />

### LocusZoom

<img src="man/figures/README-ggsci-locuszoom-1.png" width="100%" style="display: block; margin: auto;" />
Expand Down
6 changes: 6 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ reference:
- scale_color_observable
- scale_colour_observable
- scale_fill_observable
- title: "Primer"
contents:
- pal_primer
- scale_color_primer
- scale_colour_primer
- scale_fill_primer
- title: "LocusZoom"
contents:
- pal_locuszoom
Expand Down
Binary file added man/figures/README-ggsci-primer-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions man/pal_primer.Rd

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

38 changes: 38 additions & 0 deletions man/scale_primer.Rd

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

14 changes: 14 additions & 0 deletions vignettes/ggsci.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ summarized in the table below.
| Observable | `scale_color_observable()` | `"observable10"` | `pal_observable()` |
| | `scale_fill_observable()` | | |
+-----------------+------------------------------+--------------------------------+----------------------+
| Primer | `scale_color_primer()` | `"default"` | `pal_primer()` |
| | `scale_fill_primer()` | | |
+-----------------+------------------------------+--------------------------------+----------------------+
| LocusZoom | `scale_color_locuszoom()` | `"default"` | `pal_locuszoom()` |
| | `scale_fill_locuszoom()` | | |
+-----------------+------------------------------+--------------------------------+----------------------+
Expand Down Expand Up @@ -287,6 +290,17 @@ p2_observable <- p2 + scale_fill_observable()
grid.arrange(p1_observable, p2_observable, ncol = 2)
```

### Primer

The Primer palette follows the data visualization colors from [GitHub's Primer
design system](https://primer.style/product/ui-patterns/data-visualization/).

```{r}
p1_primer <- p1 + scale_color_primer()
p2_primer <- p2 + scale_fill_primer()
grid.arrange(p1_primer, p2_primer, ncol = 2)
```

### LocusZoom

The LocusZoom palette is based on the colors used by
Expand Down
Loading