如何在 R 中制作数据框的 ggdag 图(R 中的有向无环图)?
How to make a ggdag plot (Directed Acyclic Graphs in R) of data frame in R?
我有一个数据框想要在 R 中制作 ggdagplot。
这是数据样本:
structure(list(NMSUKU = c("Aceh/ Achin/ Akhir/ Asji/ A-Tse/ Ureung Aceh",
"Alas", "Aneuk Jamee", "Gayo", "Gayo Lut", "Gayo Luwes", "Gayo Serbe Jadi",
"Kluet", "Simeulue", "Simeulue", "Simeulue", "Singkil", "Tamiang"
), TopLang = c("1_Aceh/ Acheh/ Achi", "9_Alas", "4_Aceh Jamee",
"10_Gajo/ Gayo", "10_Gajo/ Gayo", "10_Gajo/ Gayo", "10_Gajo/ Gayo",
"5_Aceh Kluet", "11_Long Bano/ Simalur/ Simeuloe/ Simeulue/ Simulul",
"7_Aceh Simeleu Barat", "8_Aceh Simeleu Tengah", "3_Aceh Hulu Singkil",
"14_Tamiang"), language = c("1_Aceh/ Acheh/ Achi", "9_Alas",
"4_Aceh Jamee", "10_Gajo/ Gayo", "10_Gajo/ Gayo", "10_Gajo/ Gayo",
"10_Gajo/ Gayo", "5_Aceh Kluet", "11_Long Bano/ Simalur/ Simeuloe/ Simeulue/ Simulul",
"7_Aceh Simeleu Barat", "8_Aceh Simeleu Tengah", "3_Aceh Hulu Singkil",
"14_Tamiang")), class = "data.frame", row.names = c(NA, -13L))
这是使用 igraph
的第一种方法。您必须将可视化调整为您想要的输出..
library(igraph)
# Problem is that some column entries are similar, so they are seen
# as the same node. I added some spaces at the end of 2nd
# and 3rd columnto prevent this
df$TopLang <- paste0(df$TopLang, " ")
df$language <- paste0(df$language, " ")
# Create graph
m <- as.matrix(df)
g <- graph_from_edgelist(rbind(m[,1:2], m[,2:3]), directed = TRUE)
l <- layout_with_sugiyama(g)
# plot
plot(g, layout=-l$layout[,2:1], edge.arrow.size = 0.4)
我有一个数据框想要在 R 中制作 ggdagplot。 这是数据样本:
structure(list(NMSUKU = c("Aceh/ Achin/ Akhir/ Asji/ A-Tse/ Ureung Aceh",
"Alas", "Aneuk Jamee", "Gayo", "Gayo Lut", "Gayo Luwes", "Gayo Serbe Jadi",
"Kluet", "Simeulue", "Simeulue", "Simeulue", "Singkil", "Tamiang"
), TopLang = c("1_Aceh/ Acheh/ Achi", "9_Alas", "4_Aceh Jamee",
"10_Gajo/ Gayo", "10_Gajo/ Gayo", "10_Gajo/ Gayo", "10_Gajo/ Gayo",
"5_Aceh Kluet", "11_Long Bano/ Simalur/ Simeuloe/ Simeulue/ Simulul",
"7_Aceh Simeleu Barat", "8_Aceh Simeleu Tengah", "3_Aceh Hulu Singkil",
"14_Tamiang"), language = c("1_Aceh/ Acheh/ Achi", "9_Alas",
"4_Aceh Jamee", "10_Gajo/ Gayo", "10_Gajo/ Gayo", "10_Gajo/ Gayo",
"10_Gajo/ Gayo", "5_Aceh Kluet", "11_Long Bano/ Simalur/ Simeuloe/ Simeulue/ Simulul",
"7_Aceh Simeleu Barat", "8_Aceh Simeleu Tengah", "3_Aceh Hulu Singkil",
"14_Tamiang")), class = "data.frame", row.names = c(NA, -13L))
这是使用 igraph
的第一种方法。您必须将可视化调整为您想要的输出..
library(igraph)
# Problem is that some column entries are similar, so they are seen
# as the same node. I added some spaces at the end of 2nd
# and 3rd columnto prevent this
df$TopLang <- paste0(df$TopLang, " ")
df$language <- paste0(df$language, " ")
# Create graph
m <- as.matrix(df)
g <- graph_from_edgelist(rbind(m[,1:2], m[,2:3]), directed = TRUE)
l <- layout_with_sugiyama(g)
# plot
plot(g, layout=-l$layout[,2:1], edge.arrow.size = 0.4)