【问题标题】:Animating a Network in R在 R 中为网络设置动画
【发布时间】:2019-10-26 23:38:47
【问题描述】:

有谁知道随着网络的发展(即建立新的连接)可视化网络(来自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}")

【问题讨论】:

标签: r ggplot2 data-visualization igraph gganimate


【解决方案1】:

您可以使用ggraphpackage,它也可以与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- 函数。这里是additionalsources

【讨论】:

    猜你喜欢
    • 2012-09-23
    • 2021-09-29
    • 2020-02-02
    • 1970-01-01
    • 2011-08-11
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多