3. R可视化
g.show <- graph_from_data_frame(show_edgelist,
directed = FALSE)
E(g.show)
## + 1150/1150 edges from 2f70333 (vertex names):
## [1] 22--68 22--1 22--10 22--2 22--16 22--29 22--15 22--20 22--51 22--40
## [11] 22--17 22--45 22--44 22--42 22--32 22--12 22--25 22--11 22--3 22--63
## [21] 22--21 22--60 22--46 22--36 22--34 22--33 22--30 22--35 22--58 22--54
## [31] 22--18 22--9 22--50 22--13 22--14 22--7 22--55 22--67 22--6 22--49
## [41] 22--38 22--48 22--64 22--8 22--31 22--43 22--52 22--53 22--4 22--27
## [51] 22--59 22--23 22--19 22--41 22--71 68--10 68--2 68--16 68--29 68--15
## [61] 68--51 68--17 68--12 68--11 68--60 68--36 68--13 68--14 68--55 68--38
## [71] 68--31 68--43 68--4 68--23 68--19 68--41 24--10 24--2 24--37 24--16
## [81] 24--29 24--15 24--51 24--28 24--12 24--25 24--11 24--21 24--69 24--13
## [91] 24--14 24--5 24--65 24--61 24--38 24--4 24--23 24--19 24--41 1 --10
## + ... omitted several edges
head(as_adjacency_matrix(g.show))
## 6 x 72 sparse Matrix of class "dgCMatrix"
## [[ suppressing 72 column names '22', '68', '24' ... ]]
##
## 22 . 1 . 1 1 1 . 1 1 1 1 . 1 1 1 . 1 1 . 1 1 1 . 1 1 1 1 . 1 1 1 1 1 1 1 1 1 1
## 68 1 . . . 1 1 . 1 1 1 . . 1 . 1 . . . . . . 1 . . 1 . . . . 1 . 1 . . . . . .
## 24 . . . . 1 1 1 1 1 1 . . 1 . . 1 . . . . . 1 . 1 1 . . . 1 . . . . . . . . .
## 1 1 . . . 1 1 . 1 . 1 . . . 1 1 . 1 1 . . . 1 . . 1 . 1 . 1 . 1 . 1 1 1 . 1 .
## 10 1 1 1 1 . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2 1 1 1 1 1 . 1 1 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 . 1 1 1 1 1 1 1 1 1 1 1 1
##
## 22 . 1 1 . 1 1 1 . 1 1 . 1 . 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 . 1 1 .
## 68 . . . . . 1 1 . . 1 . . . . . 1 . . . 1 1 . . . . 1 . . 1 1 . 1 . .
## 24 . . . 1 . 1 1 1 . . 1 . 1 . . 1 . . . . . . . . . 1 . . 1 1 . 1 . .
## 1 . 1 . . . 1 1 . . . . 1 . . 1 . 1 1 1 . . . . . 1 1 1 . 1 1 . . 1 .
## 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 2 . 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 .
head(E(g.show)$Weight)
## NULL
## Original Plot
plot(g.show)

## Change node(vertex) & edge attributes
plot(g.show,
vertex.size = 12,
vertex.color = "skyblue",
vertex.frame.color = "gray0",
vertex.label.cex = 0.5,
vertex.label.color = "gray0",
edge.curved = .2,
edge.width = .7)

## Add Node Attributes to network based on ID
V(g.show)$fullname <- show_node$label[match(V(g.show)$name, show_node$id)]
V(g.show)$occupation <- show_node$occupation[match(V(g.show)$name, show_node$id)]
head(V(g.show)$fullname)
## [1] "乔振宇" "乔欣" "井柏然" "何忱" "何炅" "何舒"
head(V(g.show)$occupation)
## [1] "演员" "演员" "演员" "导演" "演员" "导演"
## Assign colors based on occupation
V(g.show)$color <- case_when(
V(g.show)$fullname %in% c("何炅", "王鸥", "张若昀", "大张伟", "魏晨", "魏大勋") ~ "antiquewhite4", # Top 5 Actors
V(g.show)$occupation %in% c("导演") ~ "azure3",
V(g.show)$occupation %in% c("编剧", "制片人") ~ "burlywood3",
TRUE ~ "antiquewhite1")
set.seed(650)
plot(g.show,
layout=layout.fruchterman.reingold,
vertex.size = 12,
vertex.frame.color = "gray0",
vertex.label.cex = .5,
vertex.label.color = "gray0",
edge.curved = .2,
edge.width = .7)
