简体   繁体   English

R中树状图的非叶节点中的标签

[英]Labels in non-leaf nodes of dendrogram in R

I want to create a graph with the classification of several items (Name1, Name2, Name3, ...) according to several criteria (Column1, Column2, Column3) in the form of a dendrogram in R (I like the orthogonal esthetics of dendrograms to this end).我想根据多个标准(Column1、Column2、Column3)以 R 中的树状图的形式创建一个包含多个项目(Name1、Name2、Name3、...)的分类图(我喜欢树状图的正交美学为此)。

I have the following CSV (data.csv) with the items and their classification in each criterion:我有以下 CSV (data.csv),其中包含每个标准中的项目及其分类:

Name;Column1;Column2;Column3
Name1;A;C;D
Name2;B;C;D
Name3;A;C;E
Name4;B;C;E
Name5;B;C;D
Name6;A;C;D
Name7;A;D;E
Name8;A;D;E
Name9;B;D;E
Name10;A;D;E

And the following R code:以及以下 R 代码:

library(data.tree)
library(DiagrammeR)
library(ggdendro)

data <- read.table(file = "data.csv", header = TRUE, sep = ";")

data$pathString <- paste("stat",
                         data$Column1,
                         data$Column2,
                         data$Column3,
                         data$Name,
                         sep = "/")

pop <- as.Node(data)
den <- as.dendrogram(pop)

p1 <- ggdendrogram(den, labels = TRUE, rotate = TRUE, leaf_labels = TRUE)
p1

Currently, I'm obtaining the following image without labels in non-leaf nodes:目前,我在非叶节点中获得以下没有标签的图像:

R中的树状图在非叶节点中没有标签

I'm wondering if it is possible to show the labels in each of non-leaf nodes in the dendrogram.我想知道是否可以在树状图中显示每个非叶节点中的标签。 For example, show "A" and "B" over each edge (related to non-leaf nodes) in decisions of Column1, show "C" and "D" over each edge in decisions of Column2, and so on.例如,在 Column1 的决策中的每条边(与非叶节点相关)上显示“A”和“B”,在 Column2 的决策中的每条边上显示“C”和“D”,依此类推。 Furthermore, it is possible to put the name of the items at the right hand of the graphic?此外,是否可以将项目名称放在图形的右侧? Thanks in advance.提前致谢。

Try using the ape package.尝试使用ape包。

# try loading a package, install if unavailable
for(i in c("ape")){
  if(!require(i, character.only = TRUE)){
    install.packages(i, dependencies = TRUE)
    library(i, character.only = TRUE)
  }
}

# convert "Node" object to class "phylo"
den2 <- as.phylo(pop)

# plot.phylo options allow flexibility in display
plot(den2, show.node.label = TRUE, label.offset = 1)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM