org_opt <- options() # store original user options options(max.print = "75") knitr::opts_chunk$set(collapse = FALSE, comment = "#>", prompt = FALSE, tidy = FALSE, echo = TRUE, message = FALSE, warning = FALSE, # Default figure options: dpi = 100, fig.align = 'center', fig.height = 5.0, fig.width = 7.5, out.width = "600px") # URLs: ------ # unicol package: url_unicol_cran <- "https://CRAN.R-project.org/package=unicol" url_unicol_github <- "https://github.com/hneth/unicol" url_github_doc_rel <- "https://hneth.github.io/unicol/" # release version url_github_doc_dev <- "https://hneth.github.io/unicol/dev/" # dev version # unikn / Uni Konstanz: url_unikn <- "https://www.uni-konstanz.de" # unikn package: url_unikn_cran <- "https://CRAN.R-project.org/package=unikn" url_unikn_github <- "https://github.com/hneth/unikn"
This vignettes shows how to use the color palettes included in the unicol R\ package. (See the All color palettes vignette for an overview of the palettes included in the package.)
We install or load the unicol package to get started:
# install.packages('unicol') # install unicol from CRAN client library('unicol') # load the package
Loading the unicol package automatically makes the color functions of the required unikn package available.
Task: Using unicol colors in visualizations created by base R graphics.
The unicol color palettes are typically provided as named vectors of color objects.
Thus, when creating base R visualizations (e.g., using plot()
for creating a scatterplot), we can directly use a color palette as the col
\ argument.
For instance, let's assume we wanted to create a bar chart using the color palette uni_bonn_2
(of the University of Bonn, Germany):
# View color palette: unikn::seecol(uni_bonn_2, main = "Color gradient of the University of Bonn, Germany")
We can simply provide the color palette to the col
argument of the barplot()
function:
# Use a color palette: barplot(1/sqrt(1:10), col = uni_bonn_2)
In case we want to modify a color palette (e.g., change its number of colors or add transparency) before using it,
we can do so by using the usecol()
function of the unikn package before providing the modified palette as the col
\ argument of an R graphics function.
The following examples illustrate both of these alternatives.
# View color palette: unikn::seecol(caltech_1, main = "The colors of Caltech, USA")
opar <- par(no.readonly = TRUE) # store current settings par(mar = c(1, 1, 2.1, 1), oma = c(0, 0, 0, 0)) # reduce margins set.seed(6 * pi) # reproducible randomness
caltech_1
): plot(x = runif(99), y = runif(99), type = "p", pch = 16, cex = 4, col = caltech_1, main = "99 dots (in Caltech colors)", axes = FALSE, xlab = NA, ylab = NA)
usecol()
function of the unikn package for resizing and adding transparency to the palette):my_col <- unikn::usecol(caltech_1, alpha = .60) # with transparency plot(x = runif(99), y = runif(99), type = "p", pch = 16, cex = 4, col = my_col, main = "99 transparent dots", axes = FALSE, xlab = NA, ylab = NA)
par(opar) # re-store original user settings
Task: Using unicol colors in visualizations created by the ggplot2 package.
When using the ggplot()
function of ggplot2 (e.g., for creating an area plot),
we wrap the color palette in the usecol()
function (of the unikn package) for defining a color palette (of the desired length and transparency).
The resulting color palette can then be provided as the values
of ggplot2's scale_color_manual()
or scale_fill_manual()
functions:
Wrap the desired color palette in the usecol()
function of unikn.
Provide this palette as the values
of the ggplot2 functions scale_color_manual()
or scale_fill_manual()
.
# 0. Create some data to plot: ---- # Example based on https://r-graph-gallery.com/137-spring-shapes-data-art/ n <- 50 groups <- 1:n df <- data.frame() set.seed(3) for (i in seq(1:30)){ data = data.frame(matrix(0, n, 3)) data[, 1] <- i data[, 2] <- sample(groups, nrow(data)) data[, 3] <- prop.table(sample(c(rep(0, 100), c(1:n)), nrow(data))) df = rbind(df, data)} names(df) <- c("X","Group","Y") df$Group <- as.factor(df$Group) df <- df[c(2, 1, 3)] df <- df[order(df$X, df$Group) , ] rownames(df) <- NULL my_data <- df # # View data (as wider table): # my_data_wider <- tidyr::pivot_wider(my_data, names_from = Group, names_prefix = "g_", values_from = Y) # knitr::kable(my_data_wider, caption = "Viewing data (in wider format).") # 1. Colors: ---- # A. Using RColorBrewer: # library(RColorBrewer) # my_pal <- brewer.pal(11, "Paired") # my_pal <- colorRampPalette(my_pal)(n) # my_pal <- my_pal[sample(c(1:length(my_pal)), size = length(my_pal))] # randomize # B. Using unicol colors: library(unicol) # Mix a color gradient: my_pal <- unikn::usecol(princeton_1, n = 50) # my_pal <- my_pal[sample(c(1:length(my_pal)), size = length(my_pal))] # randomize # 2. Plotting (with ggplot2): ---- library(ggplot2) ggplot2::ggplot(my_data, aes(x = X, y = Y, fill = Group)) + geom_area() + scale_fill_manual(values = my_pal) + theme_void() + theme(legend.position = "none")
See the Color recipes vignette (of the unikn package) for solving color-related tasks like:
The following vignettes provide an overview of and examples for using the unicol color palettes:
| Nr. | Vignette | Content |
| ---: |:---------|:-----------|
| 1. | All color palettes | The color palettes of the unicol R package |
| 2. | Using color palettes | Recipes for using the unicol color palettes |
options(org_opt) # restore original user options
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.