有谁知道一种方法来可视化网络(来自igraph
)的演变(即建立新的连接)?我看过https://www.r-graph-gallery.com/network/并在网上搜索但没有看到任何内容。
例如,如果网络是:
library("tidyverse")
library("igraph")
net.bg <- sample_pa(20)
V(net.bg)$size <- 8
V(net.bg)$label <- ""
E(net.bg)$arrow.mode <- 0
net.bg.df <- igraph::as_data_frame(net.bg)
net.bg.df <- net.bg.df %>%
mutate(time_frame = 1:n())
l <- layout_randomly(net.bg)
plot(net.bg, layout=l)
有没有一种方法可以通过字段 time_frame
来转换动画,类似于普通的情节动画:
library(ggplot2)
library(gganimate)
library(gapminder)
theme_set(theme_bw())
p <- ggplot(
gapminder,
aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)
) +
geom_point(show.legend = FALSE, alpha = 0.7) +
scale_color_viridis_d() +
scale_size(range = c(2, 12)) +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life expectancy")
p + transition_time(year) +
labs(title = "Year: {frame_time}")
最佳答案
您可以使用ggraph
package ,它也可以与 gganimate 一起使用,并且可以很好地处理 igraph 对象。
为此,我们需要指定边缘应处于事件状态的时间点。通过创建开始时间和结束点(原始数据集中的数字行)列表来完成此操作并不是很优雅。
library(tidyr)
library(ggraph)
library(gganimate)
df0 <- net.bg.df
df0$time_frame <- as.numeric(df0$time_frame)
for(i in 1:nrow(df0)){
df0$time_frame[i] <- list(df0$time_frame[i][[1]]:19)
}
df <- unnest(df0, time_frame)
g2 <- graph_from_data_frame(df)
l <- as.data.frame(l) # ggraph only accepts data.frame
colnames(l) <- c("x", "y") # ggraph needs these column names
ggraph(g2, layout = "manual", node.position = l) +
geom_node_point(color = "blue", size =3) +
geom_edge_link0(show.legend = F, width = 1) +
theme_classic() +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank()) +
transition_states(time_frame) +
ggtitle(paste0("time point: ", "{closest_state}"))
剩下的就是绘制网络并使用 transition_states
- 函数。这里是additional sources 。
关于r - 在 R 中制作网络动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56562494/