Color Palettes in R
In R, a colour palette is a collection of colours used for data visualisation, plotting, and other graphical purposes. Choosing an appropriate colour palette is crucial to creating visually appealing and informative graphs, as it helps to represent data using distinct and easily distinguishable colours.
R has several built-in colour palettes, and additional palettes are available through various R packages.
- Built-in palettes: rainbow, heat.colors, terrain.colors, or topo.colors
- Popular packages: RColorBrewer, Viridis, paletteer
Built-in palettes
R provides several built-in color palettes and functions for creating custom color palettes. Some of the most commonly used built-in color palettes in R are:
rainbow(5)
# [1] "#FF0000" "#CCFF00" "#00FF66" "#0066FF" "#CC00FF"
heat.colors(5)
# [1] "#FF0000" "#FF5500" "#FFAA00" "#FFFF00" "#FFFF80"
terrain.colors(5)
# [1] "#00A600" "#E6E600" "#EAB64E" "#EEB99F" "#F2F2F2"
topo.colors(5)
# [1] "#4C00FF" "#004CFF" "#00E5FF" "#00FF4D" "#FFFF00"
To use one of these palettes in a plot:
my_colors <- rainbow(5)
barplot(height = 1:5, col = my_colors)
Brewer palettes (from the RColorBrewer package)
The RColorBrewer
package provides a collection of color palettes that are designed to be perceptually uniform and suitable for representing data in various ways. Some popular Brewer palettes can be accessed using brewer.pal(n, "PaletteName")
, where n
is the number of colors you need, and "PaletteName"
is one of the available palette names, such as "Set1"
, "Paired"
, "Spectral"
, and more.
RColorBrewer::brewer.pal(5, "Set1")
# [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00"
To use an RColorBrewer
palette in a plot:
my_colors <- RColorBrewer::brewer.pal(3, "Set1")
pie(rep(1, 3), col = my_colors)
Viridis palettes
The viridis
package provides color palettes that are perceptually consistent and designed for colourblind people. These palettes are particularly effective for displaying sequential data. You can use functions such as viridis(n)
to generate a viridis palette with n
colors.
viridis::viridis(5)
# [1] "#440154FF" "#3B528BFF" "#21908CFF" "#5DC863FF" "#FDE725FF"
To use a viridis
palette in a plot:
# Using viridis palette
my_colors <- viridis::viridis(5)
plot(1:5, col = my_colors, pch = 16, cex = 5)
Paletteer
The ipaletteer
package provides a wide range of colour palettes from various sources, making it a versatile tool for enhancing data visualisations. Below are examples of how paletteer can be used in different scenarios:
Example #1 - To access a specific color palette, such as ronweasley2
from the harrypotter
theme, and generate a palette with 4 colors:
paletteer::paletteer_c("harrypotter::ronweasley2", n = 4)
Example #2 - To apply a color palette to a ggplot2
plot, you can use paletteer to set the colors for different categories. For instance, using the superbloom3
palette from calecopal
:
ggplot2::ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
scale_color_paletteer_d("calecopal::superbloom3")
Example #3 - Visualize Color Vision Deficiency
To ensure your color palette is accessible to individuals with color vision deficiencies, you can use the prismatic
package. For example, checking the visibility of the berlin
palette from scico
:
prismatic::check_color_blindness((paletteer_c("scico::berlin", n = 5)))
These examples demonstrate how paletteer can enhance your data visualizations with diverse color palettes and ensure they are accessible to all viewers.
Lists and Tools
- R-Color-Palettes - Comprehensive list of color palettes in r
- Interactive Color Picker - One-stop shop for R colour palettes
- prismatic - Color manipulation tools
R Graphics
R is not only a powerful tool for statistical analysis, but also a versatile platform for creating high-quality, customisable graphics. Whether you're working with simple plots or complex genomic data visualisations, R offers a wide range of packages to meet your needs. Below are some of the key resources and packages to explore for creating stunning visualisations in R:
The R Graph Gallery
The The R Graph Gallery. is a comprehensive resource showcasing hundreds of examples of R visualizations. From basic bar charts to intricate network diagrams, this gallery provides detailed code and explanations, helping you learn and implement a wide range of plotting techniques. It’s a fantastic source of inspiration for both beginners and advanced users alike.
Versatile Graphics for Genomics
For those working in genomics, gggene
and gggenome
offer specialised graphics packages that extend the popular ggplot2
framework.
gggene
: A versatile package for creating gene structure plots. It allows you to easily visualise gene models, exons and introns, providing a clear and informative view of gene architectures.gggenome
: Ideal for comparative genomics, gggenome allows you to generate comparative genome plots, visualise synteny and align genomes to explore similarities and differences. These tools are perfect for researchers who want to present complex genomic data in a meaningful, visual way.
Image Processing
The magick
package is an all-in-one toolkit for processing and manipulating images directly in R. It provides a wide range of functions for handling image formats such as PNG, JPEG and TIFF, and supports advanced image transformations, including resizing, cropping and combining multiple images. This package is essential for anyone wishing to integrate image processing into their R workflow, whether for preparing data visualisation figures, creating thumbnails, or performing detailed image analysis.
Magick Examples
# Create test directory for output
dir.create("test-magick")
# Load GDC logo from URL and resize it
logo_url <- "https://www.gdc-docs.ethz.ch/RSS/logo.svg"
logo <- magick::image_read(logo_url)
# Alternatively: logo <- magick::image_read_svg("logo.svg", width = 400)
print(logo)
# Load Professor Frink image from Wikipedia
frink_url <- "https://upload.wikimedia.org/wikipedia/en/7/71/Frink.png"
frink <- magick::image_read(frink_url)
# Alternatively: frink <- magick::image_read("frink.png")
print(frink)
# Crop a section of the GDC logo (150x250 pixels, offset by +30 pixels)
gdc_logo_cropped <- magick::image_crop(logo, "150x250+30")
print(gdc_logo_cropped)
# Change background color of the Frink image
frink_with_new_bg <- magick::image_background(frink, "#199EC2")
print(frink_with_new_bg)
# Combine the cropped logo and the modified Frink image
combined_image <- c(frink_with_new_bg, gdc_logo_cropped)
image_combined <- magick::image_append(magick::image_scale(rev(combined_image), "x300"))
print(image_combined)
# Further crop the ETH Zurich logo from the original GDC logo
ethz_logo_cropped <- magick::image_crop(logo, "200x80+170+70")
print(ethz_logo_cropped)
# Rotate and scale the Frink image
frink_transformed <- magick::image_rotate(magick::image_scale(frink_with_new_bg, "90"), 90)
print(frink_transformed)
# Combine the cropped ETH logo with the transformed Frink image
final_combination <- c(ethz_logo_cropped, frink_transformed)
final_image <- magick::image_rotate(magick::image_append(rev(final_combination), stack = TRUE), -90)
print(final_image)
These tools showcase just a fraction of the possibilities R offers for creating visually compelling and data-driven graphics. Whether you're looking to explore new techniques or delve deeper into domain-specific visualisation, the R ecosystem offers robust solutions to meet your needs.
Formatting Images and Tables in R
R provides a range of tools for organising, formatting and presenting images, tables and results in a clear and professional manner, particularly for publication-quality figures. Below are the main packages and functions for formatting visuals and output in R.
▶︎ ggpubr
: ggplot2
Based Publication Ready Plots
When working with multiple plots, the ggpubr
package provides the ggarrange()
function to arrange multiple ggplot2 objects into a single figure. This function makes it easy to combine and customise the layouts of different plots (e.g. side-by-side, stacked). It's particularly useful when preparing figures for publications or reports.
Arranging multiple plots with ggarrange()
library(ggpubr)
# Create two simple ggplots
p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p2 <- ggplot(mtcars, aes(gear, mpg)) + geom_boxplot()
# Arrange the two plots side by side
ggarrange(p1, p2, ncol = 2, nrow = 1)
▶︎ cowplot
: Streamlined plot theme and plot annotations for ggplot2
The cowplot
package is an extension to ggplot2 designed to help users produce high-quality, publication-quality plots. It provides additional tools for aligning and annotating plots, and for combining multiple plots into a single figure.
Arranging multiple plots with plot_grid()
# Create two simple ggplots
p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p2 <- ggplot(mtcars, aes(gear, mpg)) + geom_boxplot()
# Combine two plots with aligned axes
cowplot::plot_grid(p1, p2, labels = c("A", "B"))
▶︎ Pander
: A tool for rendering R objects
The pander
package facilitates the conversion of R objects, such as data frames and statistical test results, into Markdown format. This is useful when preparing reports, especially for web or document-based output (e.g. using R Markdown).
Formatting Tables with pander
Format a simple data frame into a Markdown table:
pander::pandoc.table(iris[1:3, c(1:2, 5)], style = "rmarkdown")
Output:
| Sepal.Length | Sepal.Width | Species |
|:------------:|:-----------:|:-------:|
| 5.1 | 3.5 | setosa |
| 4.9 | 3 | setosa |
| 4.7 | 3.2 | setosa |
pander
also supports formatting statistical test results, making it easy to present the results of hypothesis tests in a clean, Markdown-friendly format.
pander::pander(chisq.test(table(iris$Sepal.Length, iris$Petal.Length)), style = "rmarkdown")
Output:
| Test statistic | df | P value |
|:--------------:|:----:|:----------------:|
| 1920 | 1428 | 3.765e-17 * * * |
▶︎ broom
: Summarizes key information
The broom package takes the messy output of R's built-in functions, such as lm, nls or t.test, and turns it into tidy tibbles.
The broom
in action.
lm(formula = Sepal.Length ~ Sepal.Width, data = iris)
"Messy" Output:
Call:
lm(formula = Sepal.Length ~ Sepal.Width, data = iris)
Coefficients:
(Intercept) Sepal.Width
6.5262 -0.2234
The output of lm
is useful if you just want to read it. However, it is not trivial to convert it into tabular data that contains all the same information so that you can combine it with other models or do further analysis. You have to run coef(summary(lmfit))
to get a coefficient matrix, the terms are still stored in row names, and the column names are not consistent with other packages. Let broom
help you:
## tabular data representation
lmfit <- lm(formula = Sepal.Length ~ Sepal.Width, data = iris)
broom::tidy(lmfit)
## augment - fitted values and residuals
broom::augment(lmfit)
## glance - summary
broom::glance(lmfit)