knitr::opts_chunk$set( collapse = TRUE, comment = "#>", echo = TRUE ) library(ggplot2)
We will need the packages RColorBrewer
and pheatmap
:
RColorBrewer
and pheatmap
are installedlibrary(RColorBrewer) library(pheatmap)
We also need the "fruits" data:
data("fruits", package = "ReMUSE")
RColorBrewer
pheatmap
With integers, with color names or with an hexadecimal code (HEX)
library(kableExtra) coltab <- read.table(text = "Integer 1 2 3 4 Name black indianred2 palegreen3 dodgerblue2 HEX #000000 #DF536B #61D04F #2297E6 Integer 5 6 7 8 Name turquoise magenta3 darkgoldenrod1 gray62 HEX #28E2E5 #CD0BBC #F5C710 #9E9E9E", comment.char = "") coltab$V2 <- cell_spec(coltab$V2, color = rep(c("black", "turquoise"), each = 3)) coltab$V3 <- cell_spec(coltab$V3, color = rep(c("indianred", "magenta"), each = 3)) coltab$V4 <- cell_spec(coltab$V4, color = rep(c("palegreen", "darkgoldenrod"), each = 3)) coltab$V5 <- cell_spec(coltab$V5, color = rep(c("dodgerblue", "gray"), each = 3)) coltab %>% kable(escape = FALSE, col.names = c("", "", "", "", ""), table.attr = "style='width:100%;'", align = c("l", "c", "c", "c", "c")) %>% kable_styling(full_width = TRUE) %>% column_spec(1, bold = FALSE, border_right = TRUE, color = "white", background = "black")
The 8-color default palette in R.
barplot(rep(1,8), col = 1:8)
You can use color names (e.g. "black"
, "tomato"
, "steelblue"
, "darkorchid"
etc.)
One can access these names with the command colors()
:
sample(colors(), 7)
"Names" colors are used the same way as "integer" colors.
... with an hexadecimal code in the "Red - Green - Blue" color space:
hexdat <- data.frame( x = 1:6, col = rep(c("#FF0000", "#00FF00", "#0000FF"), each = 2) ) zesize <- 20 ggplot(hexdat, aes(x = x)) + geom_point(aes(color = I(col)), y = 1, size = zesize) + annotate("text", x = 0, y = 1, label = "#", size = zesize) + theme_minimal() + xlim(c(-0.2, 6.2)) + theme_void()
Three (almost) equivalent ways to obtain the following graph:
barplot(rep(1, 3), col = 2:4) barplot(rep(1, 3), col = c("indianred2", "palegreen3", "dodgerblue2")) barplot(rep(1, 3), col = c("#DF536B", "#61D04F", "#2297E6"))
barplot(rep(1, 3), col = 2:4)
Reproduce the graph below:
{width=80%}
If there are more objects than colors, the colors will be recycled!
par(mar = c(0, 0, 0, 0)) barplot(rep(1,80), col = 1:8, border = NA, space = 0, axes = FALSE)
We will see one package in R that contains palettes: RColorBrewer
.
The following command will display all the available palettes in RColorBrewer
:
display.brewer.all()
To get the colors from a specific palette:
brewer.pal(n = 3, name = "Set3")
{width=100%}
There are three types of palettes: sequential, diverging and qualitative.
Complete the code to obtain the graph on the right:
pal <- brewer.pal(***, ***) barplot(rep(1, 7), col = pal, axes = ***, border = ***)
{width=100%}
pheatmap
pheatmap(fruits)
Error in hclust(d, method = method) : NA/NaN/Inf dans un appel à une fonction externe (argument 10) De plus : Warning messages: 1: In dist(mat, method = distance) : NAs introduits lors de la conversion automatique 2: In dist(mat, method = distance) : NAs introduits lors de la conversion automatique
pheatmap(fruits[, -(1:2)])
pheatmap(fruits[, -(1:2)])
cluster_rows = FALSE
: remove row dendrogramscale = "column"
: standardize variablesshow_rownames = FALSE
: hide row namescellwidth = 10
: smaller cellsTo get a complete list of all arguments: ?pheatmap
pheatmap( fruits[, -(1:2)], cluster_rows = FALSE, scale = "column", show_rownames = FALSE, cellwidth = 10 )
pheatmap( fruits[, -(1:2)], cluster_rows = FALSE, scale = "column", show_rownames = FALSE, cellwidth = 10 )
colfun <- colorRampPalette( c("darkorchid", "white", "limegreen")) pheatmap( fruits[, -(1:2)], cluster_rows = FALSE, scale = "column", show_rownames = FALSE, cellwidth = 10, color = colfun(20) )
colfun <- colorRampPalette( c("darkorchid", "white", "limegreen")) pheatmap( fruits[, -(1:2)], cluster_rows = FALSE, scale = "column", show_rownames = FALSE, cellwidth = 10, color = colfun(20) )
colfun <- colorRampPalette( c("darkorchid", "white", "limegreen")) fruitsDF <- data.frame( fruits[,-1], row.names = make.unique(fruits$nom)) annotLignes <- fruitsDF[, "groupe", drop = FALSE] pheatmap( fruitsDF[, -1], cluster_rows = FALSE, scale = "column", show_rownames = FALSE, cellwidth = 10, color = colfun(20), annotation_row = annotLignes )
colfun <- colorRampPalette(c("darkorchid", "white", "limegreen")) fruitsDF <- data.frame(fruits[,-1], row.names = make.unique(fruits$nom)) annotLignes <- fruitsDF[, "groupe", drop = FALSE] pheatmap( fruitsDF[, -1], cluster_rows = FALSE, scale = "column", show_rownames = FALSE, cellwidth = 10, color = colfun(20), annotation_row = annotLignes )
Change the following command to get a pretty heatmap.
pheatmap( t(fruits), scale = "row", color = c("black", "black"), legend_breaks = c(-6, 0,+6), border_color = "pink", cellheight = 100, cellwidth = 0.1, show_colnames = "FALSE" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.