在R中对网络进行动画处理

时间:2019-06-12 12:41:15

标签: r ggplot2 data-visualization igraph gganimate

有人知道一种可视化网络(来自igraph)(随着建立新连接)的方法吗?我看过http://www.datanucleus.org/products/accessplatform/jpa/metadata_xml.html并在线搜索,但什么都没看到。

例如,如果网络是:

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}")

1 个答案:

答案 0 :(得分:1)

您可以使用resid(m1, type = "normalised") package,它也可以与ggraph一起使用并很好地处理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}")) -函数。这是additional sources

appearing edges