-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jhrcook/add-cosmic-pal
Add two palettes for COSMIC
- Loading branch information
Showing
7 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters