[英]creating nodes and edges tables for visNetwork
我怎樣才能使它更通用,例如可以應用於更多或更少列的數據表? 功能更強大,重復代碼更少。
我喜歡鏈接函數的管道代碼。 我已經走了這么遠:
df1 <- df1 %>% unique %>% apply(MARGIN = 2,FUN = function(x)return(cumsum(!duplicated(x)))) %>%
`colnames<-`(paste0("id",1:ncol(.))) %>% cbind(df1,.)
但是我正在努力鏈接下一個表達式:
df1 <- df1 %>% mutate(id2=(id2+max(id1)),
id3=(id3+max(id2)),
id4=(id4+max(id3)))
這是原始代碼。
df1 <- structure(list(step1 = c("ab1", "ab1", "ab1", "ab1", "ab1", "ab1",
"ab1", "ab1", "ab1", "ab1", "ab1", "ab1", "ab1", "ab34", "ab34",
"ab34", "ab34", "ab34", "ab34", "ab34", "ab34", "ab34", "ab34",
"ab34"), step2 = c(5000546L, 5000546L, 5000613L, 5000613L, 5000623L,
5000623L, 5000627L, 5000627L, 5000645L, 5000645L, 5000684L, 5000684L,
5000694L, 5000694L, 5000698L, 5000698L, 5000716L, 5000716L, 5000416L,
5000416L, 5000611L, 5000611L, 5000157L, 5000157L), step3 = c("500010H10A",
"500010H10B", "500010H11A", "500010H11B", "500010H12A", "500010H12B",
"500010H13A", "500010H13B", "500010H14A", "500010H14B", "500010H15A",
"500010H15B", "500010H16A", "500010H16B", "500010H17A", "500010H17B",
"500010H18A", "500010H18B", "500010H19A", "500010H19B", "500010H20A",
"500010H20B", "500010H21A", "500010H21B"), step4 = c(NA, NA,
NA, "50133B000", "50133B000", "50133B000", NA, "50138B000", "50138B000",
"50138B000", "50138B000", NA, "50138B000", "50138B000", "50138B000",
NA, "50224B000", "50224B000", NA, "50224B000", NA, "50224B000",
"50011B000", "50011B001")), .Names = c("step1", "step2", "step3",
"step4"), class = "data.frame", row.names = c(NA, -24L))
#
if (!require(dplyr)) install.packages('dplyr')
if (!require(visNetwork)) install.packages('visNetwork')
if (!require(stringr)) install.packages('stringr')
library(dplyr)
library(visNetwork)
library(stringr)
df1$id <- cumsum(!duplicated(df1$step1))
df1$id2 <- cumsum(!duplicated(df1$step2))
df1$id3 <- cumsum(!duplicated(df1$step3))
df1$id4 <- cumsum(!duplicated(df1$step4))
df1 <- df1 %>% mutate(id2=(id2+max(id)),
id3=(id3+max(id2)),
id4=(id4+max(id3)))
nodes <- data.frame(label=c(df1$step1,df1$step2,df1$step3,df1$step4) %>% str_wrap(10),
id=c(df1$id,df1$id2,df1$id3,df1$id4),
color=c(rep("red",24),rep("orange",24),rep("purple",24),rep("lightblue",24)),
level=c(rep(1,24),rep(2,24),rep(3,24),rep(4,24))) %>%
unique %>% filter(!is.na(label))
edges <- data.frame(from=c(df1$id,df1$id2,df1$id3),to=c(df1$id2,df1$id3,df1$id4)) %>% unique
visNetwork(nodes,edges,height=700, width="100%") %>%
visOptions(highlightNearest = TRUE,
collapse = TRUE) %>% visHierarchicalLayout(nodeSpacing = 300) %>% visPhysics(hierarchicalRepulsion = list(nodeDistance=135))
我對您要做什么感到有些困惑,但這是我從 Jesse Sadler 的博客 ( https://www.jessesadler.com/post/network-analysis-with-r/ ) 中采用的基本工作流程。 這是一個基於我為引文分析制作的網絡圖的示例,但在我想制作網絡圖時也適用於其他各種實例。 數據集已簡化,因此並非所有這些步驟都是真正必要的,但這是我在正常情況下會做的。
library(tidyverse)
library(visNetwork)
data <- tibble(citation = c(1:5), article = c(1:5), weight = c(1, 3, 4, 3, 6))
sources <- data %>%
distinct (citation) %>%
rename(label = citation)
destinations <- data %>%
distinct (article) %>%
rename(label = article)
nodes <- full_join(sources, destinations, by = "label") %>%
rowid_to_column("id")
edges <- data %>%
group_by(citation, article) %>%
summarize(weight = n()) %>%
ungroup() %>%
left_join(nodes, by = c("citation" = "label")) %>%
rename(from = id) %>%
left_join(nodes, by = c("article" = "label")) %>%
rename(to = id) %>%
select(edges, from, to, weight)
visNetwork(nodes, edges)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.