【发布时间】:2021-09-29 16:10:39
【问题描述】:
我有 30 个方阵形式的热图。每个热图都链接到一个日期,我想创建一个动画,根据日期从一个热图过渡到下一个。为此,我在R 中使用gganimate,但遇到了一些问题。让我用一些随机数据展示一个例子
# GENERATE RANDOM DATA. 30 3X3 MATRICES STORED IN list_matrices
pacman::p_load(tidyverse, ggplot2, av, gganimate)
list_matrices = list()
for(i in 1:30){list_matrices[[i]] = matrix(runif(9), nrow=3)}
# PUT ALL THE MATRICES TOGETHER INTO A TIBBLE AND
# DO A PIVOT LONGER IN ORDER TO USE GGPLOT
for(i in 1:length(list_matrices))
{
tmp_result = list_matrices[[i]] %>% as_tibble() %>%
mutate(rowname = c('a', 'b', 'c'),
frame = i) %>%
pivot_longer(-c(rowname, frame), names_to = 'colname')
if(i == 1)
{
df_result = tmp_result
} else{
df_result = rbind(df_result, tmp_result)
}
}
这是我将绘制的数据框:
> df_result
# A tibble: 270 x 4
rowname frame colname value
<chr> <int> <chr> <dbl>
1 a 1 V1 0.456
2 a 1 V2 0.716
3 a 1 V3 0.316
4 b 1 V1 0.724
5 b 1 V2 0.766
我创建了情节:
p <- ggplot(data = df_result, aes(x = rowname, y = colname, fill = value)) +
geom_tile() +
geom_text(aes(label = round(value, 3))) +
scale_fill_gradient(low="white", high="red", limits=c(0, 1))
p + transition_time(frame) +
labs(title = "Date: {frame_time}")
我在这里遇到三个问题
-
我将其输出到视频中,但无论 list_matrices 中的矩阵数量(即,无论我拥有多少热图),视频总是持续 10 秒,因此过渡太快了。有没有办法增加视频时长?
-
视频质量很差。有没有办法提高质量?
-
我将变量的值包含在热图中,并且我已将
geom_text(aes(label = round(value, 3)))设置为四舍五入,但视频中的某些热图显示的小数位数超过 3。知道为什么会发生这种情况以及如何解决吗?
【问题讨论】:
标签: r ggplot2 heatmap gganimate