• R语言绘制圆形树状图


    # Libraries
    library(ggraph)
    library(igraph)
    library(tidyverse)
    library(RColorBrewer) 
    # create a data frame giving the hierarchical structure of your individuals
    d1=data.frame(from="origin", to=paste("group", seq(1,10), sep=""))
    d2=data.frame(from=rep(d1$to, each=10), to=paste("subgroup", seq(1,100), sep="_"))
    edges=rbind(d1, d2)
    
    # create a vertices data.frame. One line per object of our hierarchy
    vertices = data.frame(
      name = unique(c(as.character(edges$from), as.character(edges$to))) , 
      value = runif(111)
    ) 
    # Let's add a column with the group of each name. It will be useful later to color points
    vertices$group = edges$from[ match( vertices$name, edges$to ) ]
    
    
    #Let's add information concerning the label we are going to add: angle, horizontal adjustement and potential flip
    #calculate the ANGLE of the labels
    vertices$id=NA
    myleaves=which(is.na( match(vertices$name, edges$from) ))
    nleaves=length(myleaves)
    vertices$id[ myleaves ] = seq(1:nleaves)
    vertices$angle= 90 - 360 * vertices$id / nleaves
    
    # calculate the alignment of labels: right or left
    # If I am on the left part of the plot, my labels have currently an angle < -90
    vertices$hjust<-ifelse( vertices$angle < -90, 1, 0)
    
    # flip angle BY to make them readable
    vertices$angle<-ifelse(vertices$angle < -90, vertices$angle+180, vertices$angle)
    
    # Create a graph object
    mygraph <- graph_from_data_frame( edges, vertices=vertices )
    
    # Make the plot
    ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
      geom_edge_diagonal(colour="grey") +
      scale_edge_colour_distiller(palette = "RdPu") +
      geom_node_text(aes(x = x*1.15, y=y*1.15, filter = leaf, label=name, angle = angle, hjust=hjust, colour=group), size=2.7, alpha=1) +
      geom_node_point(aes(filter = leaf, x = x*1.07, y=y*1.07, colour=group, size=value, alpha=0.2)) +
      scale_colour_manual(values= rep( brewer.pal(9,"Paired") , 30)) +
      scale_size_continuous( range = c(0.1,10) ) +
      theme_void() +
      theme(
        legend.position="none",
        plot.margin=unit(c(0,0,0,0),"cm"),
      ) +
      expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在这里插入图片描述
    参考文献:https://r-graph-gallery.com/339-circular-dendrogram-with-ggraph.html

  • 相关阅读:
    HTTP协议
    首版次高端软件申报条件和好处
    数据结构与算法知识点总结(5)查找树
    Linux 三剑客grep sed 与 awk
    tkinter的Canvas组件,绘画基本知识
    微信小程序地图功能详解
    主成分分析PCA原理以及特征
    宏工科技数智方案现先进陶瓷展,VR体验数字工厂引关注
    Linux服务器查看CPU相关信息
    vue多条件查询
  • 原文地址:https://blog.csdn.net/m0_38127487/article/details/128173657