ggraph-根据频率增加节点大小

时间:2018-11-09 03:08:52

标签: r ggplot2 ggraph

Julia Silge和David Robinson(https://www.tidytextmining.com/nasa.html)曾读过R的Tidytext Mining,并为如何根据频率(n)调整节点大小而烦恼。尝试了以下代码...

 library(widyr)
 set.seed(1234)
 title_word_pairs %>%
 filter(n >= 250) %>%
 graph_from_data_frame() %>%
 ggraph(layout = "fr") +
 geom_edge_link(aes(edge_alpha = n, edge_width = n), edge_colour = 
 "royalblue") +
 geom_node_point(aes(size = n)) + scale_size(range = c(2,10)) +
 geom_node_text(aes(label = name), repel = TRUE,
            point.padding = unit(0.2, "lines")) +
 theme_void()

...并收到此错误...

 Error: Column `size` must be a 1d atomic vector or a list
 Call `rlang::last_error()` to see a backtrace

任何想法或想法都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

问题是此频率n用于,而不是顶点。因此geom_edge_link找到n是因为n是边属性,而geom_node_point找不到n是因为它不在顶点属性中。

因此,我们希望构造另一个实际上是顶点频率的变量。

subt <- title_word_pairs %>%
  filter(n >= 250)
vert <- subt %>% gather(item, word, item1, item2) %>%
  group_by(word) %>% summarise(n = sum(n))

subt %>%
  graph_from_data_frame(vertices = vert) %>%
  ggraph(layout = "fr") +
  geom_edge_link(aes(edge_alpha = n, edge_width = n), edge_colour = "royalblue") +
  geom_node_point(aes(size = n)) + scale_size(range = c(2,10)) +
  geom_node_text(aes(label = name), repel = TRUE, point.padding = unit(0.2, "lines")) +
  theme_void()

此处subt与以前相同,则vert包含两列:顶点(单词)及其在subt中的频率作为总和或相关的边沿频率。最后,我添加了vertices = vert来传递此顶点属性。