2017-10-23 1 views
1

d3Network의 R 포트 예제를 따라 정교한 Sankey Plot을 작성하려고합니다 (여기에 명시된대로 : https://christophergandrud.github.io/networkD3/). 나는 다음과 같은 샘플 "에너지"데이터 세트로드 :은 "에너지"데이터 세트를 가져 오기NetworkD3 Sankey diagram in R : 각 링크의 가치를 계산하는 방법?

# Load energy projection data 

    URL <- paste0("https://cdn.rawgit.com/christophergandrud/networkD3/", 
    "master/JSONdata/energy.json") 

    Energy <- jsonlite::fromJSON(URL) 

두 개의 새로운 data.frames 생성을; 노드 및 링크. 링크 데이터를 살펴보면 것은 다음과 같은 형식으로 보여준다 : "소스"컬럼 유래의 노드를 나타내는

head(Energy$links) 
     source target value 
    1  0  1 124.729 
    2  1  2 0.597 
    3  1  3 26.862 
    4  1  4 280.322 
    5  1  5 81.144 
    6  6  2 35.000 

를 '값'항목의 값을 나타내는 반면, '타겟'항목은 목적지 노드를 나타낸다 각 개별 링크.

비록 개념적으로 상당히 간단하지만 나는 데이터 형식을 Energy$links data.frame과 같은 형식으로 처리하는 데 엄청난 어려움을 겪고 있습니다. 나는 다음과 같은 형식으로 내 데이터를 얻을 수 있었다, 그러나 나는 그것을 더 변환 수있는 방법에 대한 완전한 빈 그리기 오전 : 독특한 고객의 수를 집계

head(sampleSankeyData, n = 10L) 
    clientID    node1 
     <int>    <chr> 
1  23969 1 Community Services 
2  39199  1 Youth Justice 
3  23595  1 Mental Health 
4  15867 1 Community Services 
5  18295   3 Housing 
6  18295   2 Housing 
7  18295 1 Community Services 
8  18295   4 Housing 
9  15253   1 Housing 
10 27839 1 Community Services 

을 내가 할 수 있도록하려면 무엇을한다 각 링크마다. 예를 들어, 클라이언트의 18295로 인해 위의 하위 데이터 세트에서 "1 커뮤니티 서비스"에서 "2 주택"으로의 링크는 1의 값을 가져야합니다 ("2 주택"에서 "3 주택 "뿐만 아니라"3 Housing "에서"4 Housing "까지). 따라서 Sankey 다이어그램 예제에서 Energy$links과 같은 형식으로 데이터를 가져오고 싶습니다.

답변

0

이 시도 ...

library(tidyverse) 
library(stringr) 
df <- tribble(
~number, ~clientID,   ~node1, 
1 , 23969, '1 Community Services', 
2 , 39199,  '1 Youth Justice', 
3 , 23595,  '1 Mental Health', 
4 , 15867, '1 Community Services', 
5 , 18295,   '3 Housing', 
6 , 18295,   '2 Housing', 
7 , 18295, '1 Community Services', 
8 , 18295,   '4 Housing', 
9 , 15253,   '1 Housing', 
10, 27839, '1 Community Services') 

df2 <- mutate(df, step=as.numeric(str_sub(node1, end=1))) %>% 
    spread(step, node1, sep='_') %>% 
    group_by(clientID) %>% 
    summarise(step1 = sort(unique(step_1))[1], 
      step2 = sort(unique(step_2))[1], 
      step3 = sort(unique(step_3))[1], 
      step4 = sort(unique(step_4))[1]) 

df3 <- bind_rows(select(df2,1,source=2,target=3), 
      select(df2,1,source=3,target=4), 
      select(df2,1,source=4,target=5)) %>% 
    group_by(source, target) %>% 
    summarise(clients=n()) 

networkD3와 그것을 사용하는

links <- df3 %>% 
    dplyr::ungroup() %>% # ungroup just to be safe 
    dplyr::filter(!is.na(source) & !is.na(target)) # remove lines without a link 

# build the nodes data frame based on nodes in your links data frame 
nodeFactors <- factor(sort(unique(c(links$source, links$target)))) 
nodes <- data.frame(name = nodeFactors) 

# convert the source and target values to the index of the matching node in the 
# nodes data frame 
links$source <- match(links$source, levels(nodeFactors)) - 1 
links$target <- match(links$target, levels(nodeFactors)) - 1 

# plot 
library(networkD3) 
sankeyNetwork(Links = links, Nodes = nodes, Source = 'source', 
       Target = 'target', Value = 'clients', NodeID = 'name') 
관련 문제