3

新来的R;在 with 的帮助下尽力呈现我的问题reprex

我有一个使用包的节点大小按度数的网络ggraph

绘制的网络看起来不太好,因为一些节点非常小。

我想增加节点的相对大小。

igraph中,我会增加节点的相对大小,例如:

plot(df, vertex.cex=degree*5) 

我在ggraph(在下面的 rerpex 中)尝试了类似的东西,但结果是度数的乘积,而不是节点相对大小的增加。

ggraph如果只是因为整洁/语法方法和管理(哦,非常陡峭的)学习曲线,我想坚持使用该软件包(尽管如果有人对这两个软件包有一些想法,我可能会被说服)。

下面的示例没有附加图,因为我的声誉不够高,无法发布图像。但如果我做对了,reprex 应该做它应该做的事情。

#load libraries
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(ggraph)
#> Loading required package: ggplot2
# Creating a data frame
df <- rbind(c(0,1,1,0,0,1,1,0,1),
            c(0,0,1,1,0,0,1,1,0),
            c(0,1,0,0,0,0,1,0,0),
            c(0,0,0,0,0,1,0,1,0),
            c(0,0,1,0,0,1,0,1,0),
            c(0,0,1,0,0,1,1,1,0),
            c(1,0,1,1,0,1,0,1,0),
            c(0,1,0,0,0,0,0,0,1),
            c(0,1,0,0,0,1,1,0,1))
# convert to matrix
df <- as.matrix(df) #convert to matrix df; columns as headings is part of the function
# convert to tbl_graph
df <- as_tbl_graph(df)

# plot network; nodes sized by degree; nodes too small
df %>% 
  mutate(degree = centrality_degree()) %>% 
  ggraph(layout = 'kk') + 
  geom_node_point(aes(size = degree))+
  geom_edge_link()

# tried multiplying degree by a value as below; changes the 
# value of the degrees and leaves node size unchanged.
df %>% 
  mutate(degree = centrality_degree()) %>% 
  ggraph(layout = 'kk') + 
  geom_node_point(aes(size = 2*degree))+
  geom_edge_link()

reprex 包(v0.3.0)于 2020-07-18 创建

4

1 回答 1

2

我找到了一个查看此链接的解决方案:R中的网络可视化

关键是scale_size_continuous为您的情节添加管道。我试过这个选项:

df %>% 
  mutate(degree = centrality_degree()) %>% 
  ggraph(layout = 'kk') + 
  geom_node_point(aes(size = degree)) +
  scale_size_continuous(range = c(2, 5)) +
  geom_edge_link()
于 2020-07-19T08:38:26.257 回答