數(shù)據(jù)準備(數(shù)據(jù)為矩陣的表格):
windowsFonts(SH = windowsFont("Times New Roman"))#設(shè)置字體
library(openxlsx)
df=read.xlsx("data.xlsx",rowNames = T)
head(df)
美國 中國 英國 伊朗 沙特阿拉伯 澳大利亞 丹麥 越南 韓國 芬蘭 希臘
美國 21 14 1 3 3 1 1 0 0 0 0
中國 14 19 1 1 2 0 2 0 0 0 0
英國 1 1 14 7 3 4 3 3 0 2 2
伊朗 3 1 7 13 4 0 3 4 0 2 0
沙特阿拉伯 3 2 3 4 7 0 1 3 0 1 0
澳大利亞 1 0 4 0 0 5 0 0 0 0 2
構(gòu)建點邊文件:
edges=reshape2::melt(as.matrix(df))
colnames(edges)=c("from","to","r")
edges=subset(edges,!edges$r=="0") #去掉連接為0的邊
nodes=data.frame(name=unique(c(edges$from,edges$to)))
nodes$degree=degree(g)
g <- tbl_graph(nodes = nodes, edges = edges)
# 自定義顏色映射
mycol=c("#5599FF","#00AA00","#FF8888",
"#FFDD55","#DC143C","#FF8C00",
"#9ACD32", "#7700FF","#00AA88",
"#CCCCFF","#77DDFF","#FFDD65",
"#AAFFEE","steelblue","#800000")
繪制圖形
p=ggraph(g,layout='linear',circular = TRUE) +
# geom_edge_link(aes(colour = class,width=abs(r)),alpha = 0.5) +直線
# geom_edge_arc(alpha = 0.5) + #設(shè)置鏈接線為曲線,
geom_edge_arc(mapping = aes(edge_width = log10(r+1)
),edge_color = "green4",
# arrow = arrow(length = unit(4, "mm")),
start_cap = circle(3, "mm"),
end_cap = circle(3, "mm"),
alpha = 0.5) +
geom_node_point(aes(size=degree,colour = name),
alpha = 1) +
geom_node_text(aes(x = x, y=y, label=name),
angle=1,hjust=0.5, fontface="bold",size=3,family="SH") + # 設(shè)置點的注釋
scale_size_continuous(range = c(1,15)) + #設(shè)置點大小范圍
scale_color_manual(values =mycol) +
scale_edge_width_continuous(range = c(0.5,1.5)) +
theme_graph()+theme(legend.position="none")
# guides(color=F,edge_width=guide_legend(title=NULL),size=guide_legend(title=NULL))
# theme(legend.position="bottom") 圖例設(shè)置
p

1663861207234.png
@西北工作室(TB)