社区!
我有一个关于tidygraph::
和ggraph::
包的问题。我正在尝试创建一个食物网网络图(下面的代码和图表),但是,我想使用带有定义 y 轴的 kk 布局,在我的例子中是来自 nodes_df 的 TL 列。
我创建了模拟数据以作为示例显示。如果有人对如何改进这个情节有任何想法,那将有很大帮助。
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.1.2
#> Warning: package 'tibble' was built under R version 4.1.2
#> Warning: package 'readr' was built under R version 4.1.2
#> Warning: package 'purrr' was built under R version 4.1.2
#> Warning: package 'dplyr' was built under R version 4.1.2
#> Warning: package 'stringr' was built under R version 4.1.2
library(tidygraph)
#> Warning: package 'tidygraph' was built under R version 4.1.2
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
library(ggraph)
#> Warning: package 'ggraph' was built under R version 4.1.2
library(igraph)
#> Warning: package 'igraph' was built under R version 4.1.2
#>
#> Attaching package: 'igraph'
#> The following object is masked from 'package:tidygraph':
#>
#> groups
#> The following objects are masked from 'package:dplyr':
#>
#> as_data_frame, groups, union
#> The following objects are masked from 'package:purrr':
#>
#> compose, simplify
#> The following object is masked from 'package:tidyr':
#>
#> crossing
#> The following object is masked from 'package:tibble':
#>
#> as_data_frame
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
library(ggpubr)
#> Warning: package 'ggpubr' was built under R version 4.1.2
set.seed(16)
label <- c("Transient_orca",
"Salmon_sharks",
"Resident_orca",
"Sleeper_sharks",
"Halibut",
"Pinnipeds",
"Porpoise",
"Lingcod",
"Arrowtooth_L",
"Salmon_L",
"Pacific_cod",
"Sablefish",
"Arrowtooth_S",
"Spiny_dogfish",
"Avian_raptors",
"Octopods",
"Seabirds",
"Deep_demersals",
"Pollock_L",
"Rockfish",
"Baleen_whales",
"Salmon_fry_S",
"Nshore_demersal",
"Squids",
"Eulachon",
"Sea_otters",
"Deep_epibenthos",
"Capelin",
"Herring_L",
"Pollock_S",
"Invert_eat_seaduck",
"Oystercatchers",
"Sandlance",
"Sunflower_stars",
"Pisaster_Evasterias",
"Leather_stars",
"Sea_cucumbers",
"Urchins",
"Helmet_crab",
"Herring_S",
"Jellies",
"Deep_infauna_S",
"Zoopl_near_onmiv",
"Zoop_omniv",
"Shallow_infauna_S",
"Meiofauna",
"Deep_infauna_L",
"Snail_crust_S",
"Mussels",
"Barnacles",
"Shallow_infauna_clams",
"Zoopl_near_herb",
"Zoopl_herb",
"Phyto_near",
"Phyto_off",
"Fucus",
"Subtidal_kelps",
"Macroalgae_other",
"Eelgrass",
"Nekton_falls",
"Inshore_detritus",
"Offshore_detritus",
"Salmon_seine","Salmon_gillnets",
"Herring_seine","Shrimp_pot_trawl","Pollock_trawl" ,"Demersal_longline" ,
"Other_commercial","Rec_anglers","Rec_charters", "Subsistence_take")
TL <- rnorm(72, 3.155, 1)
B <- abs(rnorm(72, 0.7789, 10))
nodes_df <-data.frame(label,TL, B)
from <- rep(c("Lingcod",
"Arrowtooth_L",
"Salmon_L",
"Pacific_cod",
"Sablefish",
"Arrowtooth_S",
"Spiny_dogfish",
"Avian_raptors",
"Octopods",
"Seabirds",
"Deep_demersals",
"Pollock_L",
"Rockfish",
"Baleen_whales",
"Salmon_fry_S",
"Nshore_demersal",
"Squids",
"Eulachon",
"Sea_otters",
"Deep_epibenthos",
"Capelin",
"Herring_L",
"Pollock_S",
"Invert_eat_seaduck",
"Oystercatchers",
"Sandlance",
"Sunflower_stars",
"Pisaster_Evasterias",
"Leather_stars",
"Sea_cucumbers",
"Urchins",
"Helmet_crab",
"Herring_S",
"Jellies",
"Deep_infauna_S",
"Zoopl_near_onmiv",
"Zoop_omniv",
"Shallow_infauna_S",
"Meiofauna",
"Deep_infauna_L",
"Snail_crust_S",
"Mussels",
"Barnacles",
"Shallow_infauna_clams",
"Zoopl_near_herb",
"Zoopl_herb",
"Phyto_near",
"Phyto_off",
"Fucus",
"Subtidal_kelps",
"Macroalgae_other",
"Eelgrass",
"Nekton_falls",
"Inshore_detritus",
"Offshore_detritus"), times= 3)
to <- sample(label, size = 165, replace = TRUE)
width <- abs(rnorm(165, 0.258370, 10))
edge_df <- as_tibble(data.frame(from, to,width))
netgraph_df <- graph_from_data_frame(d=edge_df, vertices = nodes_df, directed=T)
graph <- as_tbl_graph(netgraph_df)
# plot using ggraph
ggraph(graph, layout = 'kk') +
geom_edge_link(aes(edge_width = width, color=stat(index)), lineend="round")+
scale_edge_colour_gradient(high = "#e96d50", low = "#4b84a6") +
scale_edge_width(range = c(0.5, 5)) +
geom_edge_loop(aes(edge_width = width, color=stat(index)), lineend="round")+
geom_node_point(aes(col= nodes_df$TL, size = nodes_df$B)) +
geom_node_text(aes(label = label), size=3,color="black", repel = TRUE) +
theme_transparent()+
theme(legend.position = "none")
由reprex 包创建于 2022-02-03 (v2.0.1)