if (FALSE) {
library(aqp)
library(ape)
library(cluster)
library(farver)
# load common soil mineral colors
data(soil_minerals)
# convert Munsell to R colors
soil_minerals$col <- munsell2rgb(
soil_minerals$hue,
soil_minerals$value,
soil_minerals$chroma
)
# make a grid for plotting
n <- ceiling(sqrt(nrow(soil_minerals)))
# read from top-left to bottom-right
g <- expand.grid(x=1:n, y=n:1)[1:nrow(soil_minerals),]
# convert Munsell -> sRGB -> LAB
col.rgb <- munsell2rgb(
soil_minerals$hue,
soil_minerals$value,
soil_minerals$chroma,
return_triplets = TRUE
)
# sRGB values expected to be in the range [0,255]
col.rgb <- col.rgb * 255
# convert from sRGB -> CIE LAB
col.lab <- convert_colour(
col.rgb , from = 'rgb',
to = 'lab', white_from = 'D65'
)
# keep track of soil mineral names
# in a way that will persist in a dist obj
row.names(col.lab) <- soil_minerals$mineral
# perceptual distance via CIE dE00
d <- compare_colour(
from = col.lab,
to = col.lab,
from_space = 'lab',
to_space = 'lab',
white_from = 'D65',
method = 'CIE2000'
)
# matrix -> dist
d <- as.dist(d)
# divisive hierarchical clustering of LAB coordinates
h <- as.hclust(diana(d))
p <- as.phylo(h)
# colors, in order based on clustering
# starting from top-left
min.cols <- rev(soil_minerals$col[h$order])
# mineral names, in order based on clustering
# starting from top-left
min.names <- rev(soil_minerals$mineral[h$order])
min.munsell <- rev(soil_minerals$color[h$order])
# plot grid of mineral names / colors
layout(matrix(c(1, 2), nrow = 1), widths = c(1.25, 1))
par(mar = c(1, 0, 0, 1))
plot(g$x, g$y, pch = 15, cex = 12, axes = FALSE, xlab = '', ylab = '',
col = min.cols,
xlim = c(0.5, 5.5), ylim = c(1.5, 5.5)
)
text(g$x, g$y, min.names, adj = c(0.45, 5.5), cex = 0.75, font = 2)
text(g$x, g$y, min.munsell, col = invertLabelColor(min.cols), cex = 0.85, font = 2)
title(main = 'Common Soil Pigments', line = -1.75, cex.main = 2)
mtext('U. Schwertmann, 1993. SSSA Special Publication no. 31, pages 51--69', side = 1,
cex = 0.75, line = -1.5)
# dendrogram + tip labels with mineral colors
plot(p, cex = 0.85, label.offset = 5, font = 1)
tiplabels(pch = 15, cex = 3, offset = 2, col = soil_minerals$col)
}
Run the code above in your browser using DataLab