1

我使用dendextend(vignette)编写了以下MWE:

library(cluster)
library(ggdendro)
library(dendextend)
d1 <- c(paste(rep("firstcelltype",7),1:7,sep="_"), paste(rep("secondcelltype", 3),1:3,sep="_"))
d1.df <- as.data.frame(t(combn(d1,2)))
set.seed(1)
d1.df$DIST <- sample(1:100, 45)
dmat <- with(d1.df, structure(DIST,
        Size = length(d1),
        Labels = d1,
        Diag = FALSE,
        Upper = FALSE,
        method = "user",
        class = "dist"))
c1 <- hclust(dmat, method="complete")
dend <- as.dendrogram(c1)
vals <- grep("second", d1, val=TRUE)
dend <- dend %>% set("leaves_pch", 19) %>%
        set("leaves_cex", 2) %>%
        set("by_labels_branches_col", value = vals, TF_values = c(2,Inf)) %>%
        set("by_labels_branches_lwd", value = vals, TF_values = c(4,2))
tiff("test.tiff")
mar.default <- c(5,4,4,2) + 0.1
par(mar = mar.default + c(0, 0, 0, 5)) 
plot(dend, main = "MY TITLE", xlab="my.X", horiz=TRUE)
dev.off()

我在这里所做的只是创建一个距离矩阵,其中包含“d1”中我的标签的所有可能组合以及一些随机值作为距离。

我从 hclust 结果中制作了一个树状图,并将其保存在以下 TIFF 文件中: 测试.tiff

如您所见,我已经能够更改“secondcelltype”的分支,但是如果可以的话,我需要帮助来执行以下操作:

1 - 将“secondcelltype”离开节点更改为红色方块

2 - 将“secondcelltype”标签更改为红色(可能是粗体或斜体)

3 - 到处更改字体为 Arial

我真的很感激这里的一些帮助!如果有比使用 dendextend 更好的方法,请告诉我。谢谢!

4

1 回答 1

1

一些回应:

1)这里不需要 ggdendro

2)您需要使用:

    set("labels_colors", ifelse(ss_change, 2, 1)) %>%
    set("leaves_pch", ifelse(ss_change, 15, 19)) %>%
    set("leaves_col", ifelse(ss_change, 2, 1)) 

3) 我看不到 Ariel 选项,但您可以使用“serif”、“sans”和“mono”等标准值par(family = "sans")

这是一个执行您要求的代码:

library(cluster)
library(dendextend)
d1 <- c(paste(rep("firstcelltype",7),1:7,sep="_"), paste(rep("secondcelltype", 3),1:3,sep="_"))
d1.df <- as.data.frame(t(combn(d1,2)))
set.seed(1)
d1.df$DIST <- sample(1:100, 45)
dmat <- with(d1.df, structure(DIST,
        Size = length(d1),
        Labels = d1,
        Diag = FALSE,
        Upper = FALSE,
        method = "user",
        class = "dist"))
c1 <- hclust(dmat, method="complete")
dend <- as.dendrogram(c1)
vals <- grep("second", d1, val=TRUE)

ss_change <- grepl("secondcelltype", labels(dend))

dend <- dend %>% 
        set("labels_colors", ifelse(ss_change, 2, 1)) %>%
        set("leaves_pch", ifelse(ss_change, 15, 19)) %>%
        set("leaves_col", ifelse(ss_change, 2, 1)) 
# str(unclass(dend))
# plot(dend)

dend <- dend %>% 
        set("leaves_cex", 2) %>%
        set("by_labels_branches_col", value = vals, TF_values = c(2,Inf)) %>%
        set("by_labels_branches_lwd", value = vals, TF_values = c(4,2))


par(family = "sans")
mar.default <- c(5,4,4,2) + 0.1
par(mar = mar.default + c(0, 0, 0, 5)) 
plot(dend, main = "MY TITLE", xlab="my.X", horiz=TRUE)

在此处输入图像描述

于 2016-07-15T05:07:27.657 回答