是否可以调整 ggrraph 中的相对节点大小?
Is it possible to resize the relative node size in ggrraph?
新 R
;在 with reprex
.
的帮助下尽力呈现我的问题
我有一个节点大小按度数排列的网络,使用 ggraph
包。
绘制的网络看起来不太好,因为一些节点非常小。
我想增加节点的相对大小。
在 igraph
中,我会增加节点的相对大小,例如:
plot(df, vertex.cex=degree*5)
我在 ggraph
中尝试过类似的东西(在下面的 rerpex 中),但结果是 度值 的乘积,而不是相对增加节点大小。
我想坚持使用 ggraph
包,如果只是因为 tidy/grammar 方法并管理(哦,非常陡峭的)学习曲线(尽管我可以说服其他人,如果有人对这两个包有一些想法)。
下面的例子没有附图,因为我的声望不够高post 图片。但如果我做对了,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 package (v0.3.0)
于 2020-07-18 创建
我已经找到了一个解决方案,看看这个 link:Network Visualizations in 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()
新 R
;在 with reprex
.
我有一个节点大小按度数排列的网络,使用 ggraph
包。
绘制的网络看起来不太好,因为一些节点非常小。
我想增加节点的相对大小。
在 igraph
中,我会增加节点的相对大小,例如:
plot(df, vertex.cex=degree*5)
我在 ggraph
中尝试过类似的东西(在下面的 rerpex 中),但结果是 度值 的乘积,而不是相对增加节点大小。
我想坚持使用 ggraph
包,如果只是因为 tidy/grammar 方法并管理(哦,非常陡峭的)学习曲线(尽管我可以说服其他人,如果有人对这两个包有一些想法)。
下面的例子没有附图,因为我的声望不够高post 图片。但如果我做对了,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 package (v0.3.0)
于 2020-07-18 创建我已经找到了一个解决方案,看看这个 link:Network Visualizations in 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()