使用 ggraph 绘制 bigrams 共现时出现问题,有些行不显示

Problem using ggraph for plotting bigrams co-occurrence, some lines don't show up

我正在尝试绘制双字母组的共现(来自 Text Mining with R),如下所示:

或者像这样:

但是按照书中给出的完全相同的代码,我的绘图缺少了很多线条和颜色。不确定是不是因为我错过了一些重要的步骤,或者我错过了某些包裹。

下面是一个更简单的版本:

library(dplyr)
library(ggplot2)
library(igraph)
library(ggraph)

terms <- sample(letters[1:10],50,replace=T)
count <- sample(1:50,25,replace=T)

bigrams <- data_frame(term1=terms[1:25],term2=terms[26:50],occur=count) %>%
  arrange(desc(occur)) %>%
  graph_from_data_frame()

a <- grid::arrow(type = "closed", length = unit(.15, "inches"))

我得到的图不对(甚至图例也没有正确显示):

ggraph(bigrams, layout = "fr") +
  geom_edge_link(aes(edge_alpha = occur), show.legend = FALSE,  
                 arrow = a, end_cap = circle(.07, 'inches')) +
  geom_node_point(color = "lightblue", size = 5) +
  geom_node_text(aes(label = name), vjust = 1, hjust = 1) +
  theme_void()

ggraph(bigrams, layout = "fr") +
  geom_edge_link(aes(edge_alpha = occur, edge_width = occur), edge_colour = "cyan4") +
  geom_node_point(size = 5) +
  geom_node_text(aes(label = name), repel = TRUE, 
                 point.padding = unit(0.2, "lines")) +
  theme_void()

好吧,这很有趣,但删除 theme_void() 可以解决所有问题。我想它在写这本书时做了一些不同的事情。但是第二张图中的图例仍然没有显示,所以还是有问题:

我发现 ggraph 包不错,但有一些问题。对我来说,如果你在 RStudio 中放大绘图,你的代码就可以工作。
但是,我建议您使用一些小模组,使情节无需缩放:

ggraph(bigrams, layout = "fr") +
  geom_edge_link(aes(width = occur),          # seems the alpha creates problem with legend
                 colour = "cyan4") +
  geom_node_point(size = 5) +
  scale_edge_width(range = c(0.2, 2)) +       # rescale the edges
  geom_node_text(aes(label = name), repel = TRUE, point.padding = unit(0.2, "lines"))+
 theme_graph()                                # made for graph

如果你想要 alpha,你可以试试这个,但我注意到你看到图例只在 RStudio 中放大:


数据和你的一样,只是set.seed(1)