Skip to content

Commit

Permalink
Merge pull request #1 from jhrcook/add-cosmic-pal
Browse files Browse the repository at this point in the history
Add two palettes for COSMIC
  • Loading branch information
jhrcook authored Feb 21, 2019
2 parents d02032f + b1683ec commit 513591b
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(pal_aaas)
export(pal_cosmic)
export(pal_d3)
export(pal_futurama)
export(pal_gsea)
Expand All @@ -21,6 +22,7 @@ export(pal_ucscgb)
export(rgb_gsea)
export(rgb_material)
export(scale_color_aaas)
export(scale_color_cosmic)
export(scale_color_d3)
export(scale_color_futurama)
export(scale_color_gsea)
Expand All @@ -39,6 +41,7 @@ export(scale_color_tron)
export(scale_color_uchicago)
export(scale_color_ucscgb)
export(scale_colour_aaas)
export(scale_colour_cosmic)
export(scale_colour_d3)
export(scale_colour_futurama)
export(scale_colour_gsea)
Expand All @@ -57,6 +60,7 @@ export(scale_colour_tron)
export(scale_colour_uchicago)
export(scale_colour_ucscgb)
export(scale_fill_aaas)
export(scale_fill_cosmic)
export(scale_fill_d3)
export(scale_fill_futurama)
export(scale_fill_gsea)
Expand Down
96 changes: 96 additions & 0 deletions R/discrete-cosmic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#' COSMIC Color Palettes
#'
#' Color palettes inspired by the colors used projects from the
#' \href{https://cancer.sanger.ac.uk/cosmic}{Catalogue Of
#' Somatic Mutations in Cancers (COSMIC)}
#'
#' @param palette Palette type.
#' Currently there is two available options: \code{"signature_substitutions"}
#' (6-color palette) and \code{"hallmarks"} (10-color palette).
#' @param alpha Transparency level, a real number in (0, 1].
#' See \code{alpha} in \code{\link[grDevices]{rgb}} for details.
#'
#' @export pal_cosmic
#'
#' @importFrom grDevices col2rgb rgb
#' @importFrom scales manual_pal
#'
#' @author Nan Xiao <\email{me@@nanx.me}> |
#' <\href{https://nanx.me}{https://nanx.me}>
#' @author Joshua H. Cook <\email{joshuacook0023@@gmail.com}> |
#' <\href{https://github.com/jhrcook}{GitHub/jhrcook}>
#'
#' @examples
#' library("scales")
#' show_col(pal_cosmic("hallmarks")(10))
#' show_col(pal_cosmic("hallmarks", alpha = 0.6)(10))
#' show_col(pal_cosmic("signature_substitutions")(6))
#' show_col(pal_cosmic("signature_substitutions", alpha = 0.6)(6))
pal_cosmic <- function(palette = c("hallmarks", "signature_substitutions"), alpha = 1) {
palette <- match.arg(palette)

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

raw_cols <- ggsci_db$"cosmic"[[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))
}

#' COSMIC Color Scales
#'
#' See \code{\link{pal_cosmic}} for details.
#'
#' @inheritParams pal_cosmic
#' @param ... additional parameters for \code{\link[ggplot2]{discrete_scale}}
#'
#' @export scale_color_cosmic
#'
#' @importFrom ggplot2 discrete_scale
#'
#' @author Nan Xiao <\email{me@@nanx.me}> |
#' <\href{https://nanx.me}{https://nanx.me}>
#' @author Joshua H. Cook <\email{joshuacook0023@@gmail.com}> |
#' <\href{https://github.com/jhrcook}{GitHub/jhrcook}>
#'
#' @rdname scale_cosmic
#'
#' @examples
#' library("ggplot2")
#' data("diamonds")
#'
#' ggplot(
#' subset(diamonds, carat >= 2.2),
#' aes(x = table, y = price, colour = cut)
#' ) +
#' geom_point(alpha = 0.7) +
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
#' theme_bw() + scale_color_cosmic()
#'
#' ggplot(
#' subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),
#' aes(x = depth, fill = cut)
#' ) +
#' geom_histogram(colour = "black", binwidth = 1, position = "dodge") +
#' theme_bw() + scale_fill_cosmic()
scale_color_cosmic <- function(palette = c("hallmarks"), alpha = 1, ...) {
palette <- match.arg(palette)
discrete_scale("colour", "cosmic", pal_cosmic(palette, alpha), ...)
}

#' @export scale_colour_cosmic
#' @rdname scale_cosmic
scale_colour_cosmic <- scale_color_cosmic

#' @export scale_fill_cosmic
#' @importFrom ggplot2 discrete_scale
#' @rdname scale_cosmic
scale_fill_cosmic <- function(palette = c("hallmarks"), alpha = 1, ...) {
palette <- match.arg(palette)
discrete_scale("fill", "cosmic", pal_cosmic(palette, alpha), ...)
}
Binary file modified R/sysdata.rda
Binary file not shown.
24 changes: 24 additions & 0 deletions data-raw/data-generator.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ ggsci_db$"uchicago"$"dark" <- c(
"Violet" = "#350E20"
)

# Color palette inspired by COSMIC Hallmarks of Cancer
ggsci_db$"cosmic"$"hallmarks" <- c(
"Invasion and Metastasis" = "#171717",
"Escaping Immunic Response to Cancer" = "#7D0226",
"Change of Cellular Energetics" = "#300049",
"Cell Replicative Immortality" = "#165459",
"Suppression of Growth" = "#3F2327",
"Genome Instability and Mutations" = "#0B1948",
"Angiogenesis" = "#E71012",
"Escaping Programmed Cell Death" = "#555555",
"Proliferative Signaling" = "#193006",
"Tumour Promoting Inflammation" = "#A8450C"
)

# Color palette inspired by COSMIC Hallmarks of Cancer
ggsci_db$"cosmic"$"signature_substitutions" <- c(
"C>A" = "#5ABCEB",
"C>G" = "#050708",
"C>T" = "#D33C32",
"T>A" = "#CBCACB",
"T>C" = "#ABCD72",
"T>G" = "#E7C9C6"
)

# Color palette inspired by The Simpsons
ggsci_db$"simpsons"$"springfield" <- c(
"HomerYellow" = "#FED439", "HomerBlue" = "#709AE1",
Expand Down
36 changes: 36 additions & 0 deletions man/pal_cosmic.Rd

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

53 changes: 53 additions & 0 deletions man/scale_cosmic.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 @@ -76,6 +76,9 @@ summarized in the table below.
| | `scale_fill_uchicago()` | `"light"`<br> | |
| | | `"dark"` | |
+-----------------+------------------------------+--------------------------------+----------------------+
| COSMIC | `scale_color_cosmic()` | `"hallmarks"` | `pal_cosmic()` |
| | `scale_fill_cosmic()` | `"signature_substitutions"` | |
+-----------------+------------------------------+--------------------------------+----------------------+
| Star Trek | `scale_color_startrek()` | `"uniform"` | `pal_startrek()` |
| | `scale_fill_startrek()` | | |
+-----------------+------------------------------+--------------------------------+----------------------+
Expand Down Expand Up @@ -263,6 +266,17 @@ p2_uchicago <- p2 + scale_fill_uchicago()
grid.arrange(p1_uchicago, p2_uchicago, ncol = 2)
```

## COSMIC

# Color palette inspired by [COSMIC](https://cancer.sanger.ac.uk/cosmic) Hallmarks of Cancer (`"hallmarks"`) and the mutational signature substitution (`"signature_substitutions"`) colors.

```{r, fig.width = 10.67, fig.height = 4, out.width = 800, out.height = 300, dpi = 150}
p1_cancer_hallmarks <- p1 + scale_color_cosmic()
p2_cancer_hallmarks <- p2 + scale_fill_cosmic()
grid.arrange(p1_cancer_hallmarks, p2_cancer_hallmarks, ncol = 2)
```


## Star Trek

This palette is inspired by the (uniform) colors in <emph>Star Trek</emph>:
Expand Down

0 comments on commit 513591b

Please sign in to comment.