pvclust编辑树状图

时间:2018-10-11 10:08:24

标签: r pvclust

我正在进行一些聚类分析,并且正在使用pvclust,如下所示:

d.pv <- pvclust(t(mtcars), method = "euclidean", 
            method.hclust = "complete", nboot = 10)
plot(d.pv)

我要编辑图形并删除红色,绿色和灰色数字。 我也想根据特定的列mtcars$cyl

在x轴上给标签加色

1 个答案:

答案 0 :(得分:1)

要删除红色,绿色和灰色数字,请执行以下操作:

plot(d.pv, print.num = FALSE, print.pv = FALSE)

plot.pvclust的范围内标记颜色比较麻烦。我建议将其转换为ggplot2,以提高灵活性。

# Run pvclust and restructure data
d.pv <- as.dendrogram(pvclust(t(mtcars), method = "euclidean", 
                      method.hclust = "complete", nboot = 10)$hclust)

ddata <- dendro_data(d.pv, type = "rectangle")

# Get data frames to plot
df_seg <- segment(ddata)
df_labs <- data.frame(label(ddata), cyl = as.factor(mtcars[match(label(ddata)$label, rownames(mtcars)), "cyl"]))

# Create ggplot dendrogram
p <- ggplot()
p <- p + geom_segment(data = df_seg,
                      aes(x = x, y = y, xend = xend, yend = yend),
                      size = 1.25,
                      colour = "darkgray",
                      lineend = "round")
p <- p + geom_text(data = df_labs, 
                   aes(x = x, 
                       y = y, 
                       label = label,
                       colour = cyl), 
                   nudge_y = -10,
                   family = "serif",
                   size = 5,
                   angle = 90,
                   hjust = 1)
p <- p + xlab("") + ylab("Height")
p <- p + theme(axis.line.x = element_blank(),
               axis.text.x = element_blank(),
               axis.ticks.x = element_blank(),
               text = element_text(family = "serif"))
p <- p + scale_y_continuous(expand = expand_scale(add = c(85, 0)))
p

enter image description here