天天看點

R語言網絡圖的一個小例子

資料連結

http://www.kateto.net/wordpress/wp-content/uploads/2019/06/sunbelt2019.zip

參考資料

https://kateto.net/network-visualization

代碼

nodes<-read.csv("Data_files/Dataset1-Media-Example-NODES.csv",
                header=T,as.is = T)
links<-read.csv("Data_files/Dataset1-Media-Example-EDGES.csv",
                header=T,as.is=T)
nodes
links
dim(nodes)
dim(links)
library(igraph)
net<-graph_from_data_frame(d=links,
                           vertices=nodes,directed=T)
net
library(ggraph)
ggraph(net)+
  geom_edge_link(color="green")+
  geom_node_point(color="red",size=10)+
  theme_void()
           

複制

R語言網絡圖的一個小例子

image.png

ggraph(net,layout="lgl")+
  geom_edge_link()+
  theme_void()
           

複制

R語言網絡圖的一個小例子

image.png

ggraph(net,layout="lgl")+
  geom_edge_fan()+
  geom_node_point(color=V(net),size=8)+
  theme_void()
           

複制

R語言網絡圖的一個小例子

image.png

ggraph(net, layout = 'linear') +
  geom_edge_arc(color = "orange", width=0.7) +
  geom_node_point(size=5, color="gray50") +
  theme_void()
           

複制

R語言網絡圖的一個小例子

image.png

ggraph(net, layout="lgl") +
  geom_edge_link(aes(color = type)) + # colors by edge type
  geom_node_point(aes(size = audience.size)) + # size by audience size
  theme_void()
           

複制

R語言網絡圖的一個小例子

image.png

ggraph(net, layout = 'lgl') +
  geom_edge_arc(color="gray", curvature=0.3) +
  geom_node_point(color="orange", aes(size = audience.size)) +
  geom_node_text(aes(label = media), size=2, color="gray50", repel=T) +
  theme_void()
           

複制

R語言網絡圖的一個小例子

image.png

教程中的例子可以重複出來,但是如何準備自己的資料呢?

嘗試模仿

df1<-data.frame(id=c("A","B","C"))
df2<-data.frame(from=c("A","A","B","C"),
                to=c("B","C","C","B"),
                type=c("hyperlink","hyperlink","mention","mention"),
                weight=c(30,10,20,50))
net1<-graph_from_data_frame(d=df2,vertices = nodes)
           

複制

遇到報錯

Error in graph_from_data_frame(d = df2, vertices = nodes) : 
  Some vertex names in edge list are not listed in vertex data frame
           

複制

暫時還不知道如何解決!