How to put italic font in dendrogram using factoextra package?

前提是你 提交于 2020-05-17 06:06:26

问题


Using the function fviz_dend() of the factoextra package I created a dendogram that can be seen in the image at the end of the question.

However, I cannot put the names of each species in italics. I tested the element_text(face = 'italic') function but it only works for the y-axis title. If anyone has any suggestions for leaving the species name in italics I thank.

I believe it is something involving the fviz_dend() function but I have not found anything about it. Below it is possible to find the code I used, the image generated and some links I accessed in the hope of finding something about it.

Building table and preparing data to plot the dendrogram:

# 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" )

Plotting the dendrogram:

# 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)

Dendrogram

Some sources I searched for:

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


回答1:


I got the solution in a very similar post:

How to make chart fonts in italics? (ggplot2 and factoextra package)

Below is the solution:

# 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)


来源:https://stackoverflow.com/questions/61602871/how-to-put-italic-font-in-dendrogram-using-factoextra-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!