1. 程式人生 > >複雜樹狀網路的視覺化

複雜樹狀網路的視覺化

 

image

基於R語言networkD3包實現,可以非常方的畫一般的樹狀圖或者層次聚類數狀圖,很多時候,需要表達關聯關係或者層次關係,樹狀圖是一種非常好的視覺化圖形。下面教大家怎麼構造資料結構並畫出圖形。

一、先看一個官方的例子

Flare <- jsonlite::fromJSON("https://gist.githubusercontent.com/mbostock/4063550/raw/a05a94858375bd0ae023f6950a2b13fac5127637/flare.json",simplifyDataFrame = FALSE)

radialNetwork(List = Flare, fontSize = 10, opacity = 0.9, margin=0,nodeColour = "#00E5EE",linkColour = "#00EEEE")

image

diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9, margin=0,nodeColour = "#00E5EE",linkColour = "#00EEEE")

image

二、看看怎麼畫的

案例1:使用者關聯的裝置,然後關聯新使用者

畫這個圖的難點在於資料結構的構造,需要用到R語言的列表,入門的時候不是很理解,其實列表就是向量的一種變形,下面看個非常簡單的例子:

user-裝置-user1

                -user2

                -user3

就是通過一個使用者關聯出裝置,然後用裝置關聯出新的人,看看資料結構是怎麼構造的

USER <- list(name = "user", children = list(

               list(name = "裝置",

                    children = list(list(name = "user1"),

               list(name = "user2"),

               list(name = "user3")

))))

diagonalNetwork(List = USER, fontSize = 10, opacity = 0.9, margin=0,nodeColour = "#00E5EE",linkColour = "#00EEEE")

image

       

radialNetwork(List = USER, fontSize = 10, opacity = 0.9, margin=0,nodeColour = "#00E5EE",linkColour = "#00EEEE")

image

再複雜點,多加兩個裝置

USER <- list(name = "user", children = list(

               list(name = "裝置1",

                    children = list(list(name = "user1"),

               list(name = "user2"),

               list(name = "user3")

)),

               list(name = "裝置2",

                    children = list(list(name = "user1"),

               list(name = "user2"),

               list(name = "user3")

)),

               list(name = "裝置3",

                    children = list(list(name = "user1"),

               list(name = "user2"),

               list(name = "user3")

))))

diagonalNetwork(List = USER, fontSize = 10, opacity = 0.9, margin=0,nodeColour = "#00E5EE",linkColour = "#00EEEE")

image

       

radialNetwork(List = USER, fontSize = 10, opacity = 0.9, margin=0,nodeColour = "#00E5EE",linkColour = "#00EEEE")

image

案例2:一個更復雜點的例子

CanadaPC <- list(name = "Canada", children = list(

               list(name = "Newfoundland",

                    children = list(list(name = "St. John's"))),

               list(name = "PEI",

                    children = list(list(name = "Charlottetown"))),

               list(name = "Nova Scotia",

                    children = list(list(name = "Halifax"))),

               list(name = "New Brunswick",

                    children = list(list(name = "Fredericton"))),

               list(name = "Quebec",

                    children = list(list(name = "Montreal"),

                                    list(name = "Quebec City"))),

               list(name = "Ontario",

                    children = list(list(name = "Toronto"),

                                    list(name = "Ottawa"))),

               list(name = "Manitoba",

                    children = list(list(name = "Winnipeg"))),

               list(name = "Saskatchewan",

                    children = list(list(name = "Regina"))),

               list(name = "Nunavuet",

                    children = list(list(name = "Iqaluit"))),

               list(name = "NWT",

                    children = list(list(name = "Yellowknife"))),

               list(name = "Alberta",

                    children = list(list(name = "Edmonton"))),

               list(name = "British Columbia",

                    children = list(list(name = "Victoria"),

                                    list(name = "Vancouver"))),

               list(name = "Yukon",

                    children = list(list(name = "Whitehorse")))

))

image

radialNetwork(List = CanadaPC, fontSize = 15, opacity = 0.9, margin=0,nodeColour = "#00E5EE",linkColour = "#00EEEE")

image

在層次聚類中,每個觀測值自成一類,這些類每次兩兩合併,直到所有的類被聚成一類為止。常用的有單聯動、全聯動、平均聯動、質心以及Ward方法。

使用的資料是,USArrests,結構如下:

image

#進行聚類

hc <- hclust(dist(USArrests), "ave")

radialNetwork(as.radialNetwork(hc))

image

diagonalNetwork(as.radialNetwork(hc), height = 700, margin = 50)

image

dendroNetwork(hc, height = 600)

image

dendroNetwork(hc, treeOrientation = "vertical")

image

dendroNetwork(hc, height = 600, linkType = "diagonal")

image

dendroNetwork(hc, treeOrientation = "vertical", linkType = "diagonal")

image

dendroNetwork(hc, textColour = c("red", "green", "orange")[cutree(hc, 3)],height = 600)

image

dendroNetwork(hc, textColour = c("red", "green", "orange")[cutree(hc, 3)], treeOrientation = "vertical")

image

轉載:https://paper.tuisec.win/detail/78a9d550ee4e1e8