本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

Analysis in R: Helps color labels in dendrograms. The “colorhcplot” package

This package is useful for color-coding dendrogram labels in groups.

Package version is 1.3.. Checked with R version 4.1.2.

Install Package

Run the following command.

#Install Package
install.packages("colorhcplot")
スポンサーリンク

Example

See the command and package help for details.

#Loading the library
library("colorhcplot")

###Creating Data#####
set.seed(1234)
n <- 10
TestData <- data.frame(row.names = paste0("ID", 1:n),
                       Group = as.factor(sample(paste0("Group", 1:3),
                                                n, replace = TRUE)),
                       Test_A = rnorm(n), Test_B = rnorm(n), Test_C = rnorm(n))
########

#Distance Calculation
#Loading the amap package to use the Spearman method
#Install the amap package if it is not already present
if(!require("amap", quietly = TRUE)){
  install.packages("amap");require("amap")
}
#Calculation
DistData <- Dist(TestData[, -1], method = "spearman")

#Clustring
#method option:"ward.D","ward.D2","single","complete","average",
#"mcquitty","median","centroid"
hTestData <- hclust(DistData, method = "complete")
########

#Plot the dendrogram: colorhcplot command
#Data:hc option
#Specify group data:fac option
#Specify hang value:hang option: negative value makes the label bottom
#Direction of y-axis labels:las option; 1:vertical,0:horizontal
colorhcplot(hc = hTestData, fac = TestData[, 1],
            hang = -1, main = "Karada Good",
            lab.cex = 1.3, lwd = 2, las = 1,
            color = c("chartreuse2", "orange2", "blue"))

Output Example

・colorhcplot command

I hope this makes your analysis a little easier !!

スポンサーリンク
Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.