2016-09-18 126 views
1

我想用networkD3::forceNetwork來創建僱主和學院的圖表,僱主僱用僱員。如何創建網絡圖表?

現在,我有這樣的事情:

forceNetwork(Links= Links, Nodes= netDf , 
      Source = 'collegeName', Target = 'organizationName', Value='count', 
      NodeID = 'collegeName', Group = 'organizationName') 

但作爲預期的輸出不看。我想有,是:

每個大學
  1. 一個氣泡
  2. 每個僱主
  3. 院校連接到僱主,僱主的數量的一種泡沫( count)映射到連接線的寬度。

學院從來沒有相互連接,並且同樣適用於僱主。

這是我使用netDf數據集:

structure(list(collegeName = c("college1", "college1", "college2", 
"college3", "college3", "college3", "college4", "college5", "college5", 
"college6", "college6", "college6", "college7", "college7", "college7", 
"college8", "college9", "college10", "college10", "college11" 
), organizationName = c("employer2", "employer3", "employer2", 
"employer1", "employer2", "employer3", "employer2", "employer2", 
"employer3", "employer1", "employer2", "employer3", "employer1", 
"employer2", "employer3", "employer2", "employer2", "employer2", 
"employer3", "employer2"), count = c(858, 176, 461, 201, 2266, 
495, 430, 1992, 290, 127, 1754, 549, 136, 2839, 686, 638, 275, 
1388, 387, 188), group = c(2, 3, 2, 1, 2, 3, 2, 2, 3, 1, 2, 3, 
1, 2, 3, 2, 2, 2, 3, 2)), .Names = c("collegeName", "organizationName", 
"count", "group"), row.names = c(NA, -20L), class = "data.frame") 

這是Links數據集:

structure(list(collegeName = c(0, 0, 1, 2, 2, 2, 3, 4, 4, 5, 
5, 5, 6, 6, 6, 7, 8, 9, 9, 10), organizationName = c(1, 2, 1, 
0, 1, 2, 1, 1, 2, 0, 1, 2, 0, 1, 2, 1, 1, 1, 2, 1), count = c(858, 
176, 461, 201, 2266, 495, 430, 1992, 290, 127, 1754, 549, 136, 
2839, 686, 638, 275, 1388, 387, 188), group = c(2, 3, 2, 1, 2, 
3, 2, 2, 3, 1, 2, 3, 1, 2, 3, 2, 2, 2, 3, 2)), .Names = c("collegeName", 
"organizationName", "count", "group"), row.names = c(NA, -20L 
), class = "data.frame") 

此外,纔有可能映射一個4變量泡沫大小?舉例來說,我想將count映射到與僱員有關的泡沫大小,我該怎麼做?

回答

3

我認爲你的LinksNodes數據幀不符合?forceNetwork中規定的要求。相反,你可以這樣做:

library(networkD3) 
set.seed(1) 

nodes <- data.frame(Label = unique(c(netDf[,1], netDf[,2]))) 
nodes$Group <- as.factor(substr(nodes$Label, 1, 3)) 
nodes <- merge(
    nodes, 
    aggregate(count~organizationName, netDf, sum), 
    by.x="Label", by.y="organizationName", 
    all.x=TRUE 
) 
nodes$count[is.na(nodes$count)] <- 1 

links <- transform(netDf, 
    Source = match(netDf$collegeName, nodes$Label)-1, 
    Target = match(netDf$organizationName, nodes$Label)-1 
) 

forceNetwork(
    Links = transform(links, count = count/min(count)), 
    Nodes = nodes, 
    Source = 'Source', 
    Target = 'Target', 
    Value='count', 
    NodeID = 'Label', 
    Group = "Group", 
    Nodesize = "count", 
    legend = TRUE, 
    opacity = 1, 
    radiusCalculation = JS("Math.log(d.nodesize)+6") 
) 

enter image description here

+0

是否存在被集團,集羣它們例如具有在中心的所有'col'和'emp'左右'col'浮動任何簡單的方法? – Dambo