r - R:如何控制 ggraph 中边缘的行为
问题描述
我正面临这个问题:我得到了一些这样的数据:
library(tidyverse)
library(tidygraph)
library(ggraph)
library(ggrepel)
edges <- data.frame(a=c('k','k','k','k','k','z','z'),
b=c('b','b','b','b','c','b','c'), costant = 1)
a b costant
1 k b 1
2 k b 1
3 k b 1
4 k b 1
5 k c 1
6 z b 1
7 z c 1
现在我希望有一个带有 ggraph 的图,它有节点和带权重的边。所以我是这样工作的:
# first I calculated the edges weights
edges1 <- edges%>% group_by(a,b) %>% summarise(weight = sum(costant))
> edges1
# A tibble: 4 x 3
# Groups: a [?]
a b weight
<fct> <fct> <dbl>
1 k b 4
2 k c 1
3 z b 1
4 z c 1
然后节点:
nodes <- rbind(data.frame(word = edges$a, n = 1),data.frame(word = edges$b, n = 1)) %>%
group_by(word) %>%
summarise(n = sum(n))
> nodes
# A tibble: 4 x 2
word n
<fct> <dbl>
1 k 5
2 z 2
3 b 5
4 c 2
到目前为止,一切正常。现在,以此为例:
tidy <- tbl_graph(nodes = nodes, edges = edges1, directed = T)
tidy <- tidy %>%
activate(edges) %>%
arrange(desc(weight)
)
突然我绘制了图表:
ggraph(tidy, layout = "gem") +
geom_node_point(aes(size=n)) +
geom_edge_link(aes(width = weight), alpha = 0.8) +
scale_edge_width(range = c(0.2, 2)) +
geom_text_repel(aes(x = x, y=y , label=word))
但结果是这样的:
而且我无法弄清楚为什么k和z之间有一条线,因为边不存在。
预先感谢。
解决方案
似乎是由于在不考虑tibble 的情况下tbl_graph
将edge1
tibble 的节点从转换factor
为integer
by的事实,这是错误的根源。如果我们正确地将边缘节点预转换为整数,它将按预期工作。as.integer
nodes
edges <- data.frame(a=c('k','k','k','k','k','z','z'),
b=c('b','b','b','b','c','b','c'), costant = 1)
edges1 <- edges%>% group_by(a,b) %>% summarise(weight = sum(costant))
nodes <- rbind(data.frame(word = edges$a, n = 1),data.frame(word = edges$b, n = 1)) %>%
group_by(word) %>%
summarise(n = sum(n))
edges2 <- edges1 # save edges with factor node labels into edge2
# convert 'from' and 'to' factor columns to integer columns correctly
# with the nodes tibble's corresponding matched index values
edges1$a <- match(edges1$a, nodes$word)
edges1$b <- match(edges1$b, nodes$word)
tidy <- tbl_graph(nodes = nodes, edges = edges1, directed = T)
tidy <- tidy %>%
activate(edges) %>%
arrange(desc(weight)
)
ggraph(tidy, layout = "gem") +
geom_node_point(aes(size=n)) +
geom_edge_link(aes(width = weight), arrow = arrow(length = unit(4, 'mm')), end_cap = circle(3, 'mm'), alpha = 0.8) +
scale_edge_width(range = c(0.2, 2)) +
geom_text_repel(aes(x = x, y=y , label=word))
edges2 # compare the edges in the following tibble with the next figure
# A tibble: 4 x 3
# Groups: a [?]
a b weight
<fct> <fct> <dbl>
#1 k b 4
#2 k c 1
#3 z b 1
#4 z c 1
推荐阅读
- drag-and-drop - Sortablejs 禁止从列表拖动到外部编辑器
- python - 导入模块后tensorflow导入报错的解决方法
- visual-studio-code - 在 vscode 中禁用特定的键绑定
- google-chrome-extension - chrome扩展和pwa之间如何通信?
- c - 将指针传递给递归函数,该函数最初从 main - cast 传递给来自不同大小的整数的指针——C
- spring - 在编译 Spring mvc 示例时,在 pom.xml 中获取“无法调用“java.net.URI.getScheme()”,因为“uri”为空”
- python-3.x - 获取用户输入并在 Turtle Screen 上显示
- java - Jacoco 无法使用
在 pom.xml 中 - nginx - 如何优化我的 nginx 配置
- python - 如何从我的 txt 文件中搜索和显示所有记录?