如何使用 factoextra 包将斜体字体放入树状图中?

How to put italic font in dendrogram using factoextra package?

使用 factoextra 包的函数 fviz_dend() 我创建了一个树状图,可以在问题末尾的图像中看到。

但是,我不能将每个物种的名称都用斜体表示。我测试了 element_text(face = 'italic') 函数,但它仅适用于 y-axis 标题。如果有人对将物种名称保留为斜体有任何建议,我感谢。

我相信它涉及 fviz_dend() 函数,但我还没有找到任何相关信息。下面可以找到我使用的代码、生成的图像和我访问的一些链接,希望能找到一些相关信息。

构建 table 并准备绘制树状图的数据:

# creating variables
Saccharomyces_cerevisiae <- c(67, 37, 88, 15, 46)
Drosophila_melanogaster <- c(134, 121, 93, 133, 104)
Caenorhabditis_elegans <- c(160, 169, 182, 179, 174)
Xenopus_tropicalis <- c(197, 200, 154, 165, 163)
Mus_musculus <- c(67, 48, 64, 16, 36)
Danio_rerio <- c(176, 189, 200, 152, 184)

# creating data.frame
df <- data.frame(Saccharomyces_cerevisiae,
                 Drosophila_melanogaster,
                 Caenorhabditis_elegans,
                 Xenopus_tropicalis,
                 Mus_musculus,
                 Danio_rerio)

# naming the lines
rownames(df) <- c("ion1","ion2","ion3","ion4","ion5")

# standardizing by columns
df <- scale(df)

# converting columns to rows
df <- t(df)

# generating distance matrix of lines
df.dist <- dist(df, method = "euclidean")

# cluttering
hca <- hclust(df.dist, method = "ward.D2" )

绘制树状图:

# Loading packages
library(factoextra)
library(ggplot2)

# Creating dendrogram
fviz_dend(hca,
          cex = 0.4,
          k = 2,
          labels_track_height = -0.485,
          k_colors = c("blue","#006600"),
          rect = TRUE,
          rect_fill = TRUE, 
          rect_border = c("blue","#006600"),
          color_labels_by_k = TRUE,
          main = "",
          ylab = "Linkage distance",
          xlab = "") +
  scale_y_continuous(breaks = seq(-12, 5, by = 1), limits=c(-12,5)) +
  theme_classic() + 
  theme(axis.title.y = element_text(size = rel(0.7), color = "black", face = 'italic'),
        axis.title.x = element_text(size = rel(0.7), color = "black"),
        axis.text.x = element_text(size = rel(0.5),color = "black"),
        axis.text.y = element_text(size = rel(0.5),color = "black")) +
  ggsave("dendrogram.png", width = 5, height = 5, units = "cm", dpi=300)

树状图

我搜索的一些来源:

https://www.rdocumentation.org/packages/factoextra/versions/1.0.7/topics/fviz_dend

https://cran.r-project.org/web/packages/factoextra/factoextra.pdf

https://www.datanovia.com/en/lessons/examples-of-dendrograms-visualization/

http://www.sthda.com/english/wiki/beautiful-dendrogram-visualizations-in-r-5-must-known-methods-unsupervised-machine-learning

我得到的解决方案非常相似 post:

解决方法如下:

# Loading packages
library(factoextra)
library(ggplot2)

# creating dendrogram
graph <- fviz_dend(hca,
          cex = 0.4,
          k = 2,
          labels_track_height = -0.485,
          k_colors = c("blue","#006600"),
          rect = TRUE,
          rect_fill = TRUE, 
          rect_border = c("blue","#006600"),
          color_labels_by_k = TRUE,
          main = "",
          ylab = "Linkage distance",
          xlab = "") +
  scale_y_continuous(breaks = seq(-12, 5, by = 1), limits=c(-12,5)) +
  theme_classic() + 
  theme(axis.title.y = element_text(size = rel(0.7), color = "black", face = 'italic'),
        axis.title.x = element_text(size = rel(0.7), color = "black"),
        axis.text.x = element_text(size = rel(0.5),color = "black"),
        axis.text.y = element_text(size = rel(0.5),color = "black"))

# solution
graph$layers[[2]]$aes_params$fontface <- "italic"

# plotting
graph

# saving
ggsave("dendrogram.png", width = 5, height = 5, units = "cm", dpi=300)