我的任务是创建树状图,但叶节点显示钝边。我将如何扩展叶节点的长度并添加节点标签?
请参阅下面的当前和预期图像。
数据:
df1 <- data.frame( z1 = c(rep('P1', 5), rep('P2', 5), rep('P3', 3), rep('P4', 4)),
z2 = c(letters[1:5], letters[6:10], letters[11:13], letters[14:17]),
stringsAsFactors = FALSE)
代码:
library('data.table')
library('ggplot2')
library('ggdendro')
library('grid')
setDT(df1)
ddata <- dcast( data = df1[, .(z1, z2)],
formula = z2 ~ z1,
fill = 0,
fun.aggregate = length,
value.var = 'z2')
setDF( ddata)
row.names(ddata) <- ddata$z2
ddata$z2 <- NULL
ddata <- dendro_data( as.dendrogram( hclust( dist( ddata), method = "average")))
p <- ggplot(segment(ddata)) +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
theme_dendro()
print(p)
当前情节:
预期剧情: