gpt4 book ai didi

r - gganimate 中的 Ghost geom_text

转载 作者:行者123 更新时间:2023-12-05 07:07:36 25 4
gpt4 key购买 nike

我正在玩弄 gganimate,我相信它在标签方面有点古怪(我基本上遵循了 this 示例)。

我正在使用此代码片段生成以下 .gif(您可以找到数据 here,不希望帖子长度爆炸)。

library(gganimate)
library(dplyr)

df <- read.csv("https://pastebin.com/raw/QvhdVqwM", stringsAsFactors = FALSE) %>%
mutate(date = as.Date(date))

countries_anim <- df %>%
filter(country_code == "de") %>%
ggplot(aes(date, value, colour = city_name)) +
geom_line() +
geom_segment(aes(xend = max(date) - 30, yend = value), linetype = 2,
colour = "grey") +
geom_text(aes(x = max(date) - 29, label = city_name), hjust = 0) +
theme(legend.position = "bottom") +
guides(colour = guide_legend(title.position = "top")) +
transition_reveal(date)

n_days <- as.integer(max(df$date) - min(df$date))

anim <- animate(plot = countries_anim, duration = 10,
renderer = gifski_renderer(file = 'figures/de.gif'))

除了一个小问题外,一切都很好:在动画的一开始,一些注释(应该遵循时间序列趋势)永久打印在绘图区域中。我尝试更改渲染器,但问题似乎完全不相关。

enter image description here

我不是很精通 gganimate 的内部机制,我想知道如何调试这个问题。

最佳答案

我已经苦苦调试了几个小时,但我似乎找到了解决方案。显然,动画注释会受到数据排序方式的影响;正如您在下面的示例中看到的,我的数据集按降序排列(按 date)。更改顺序似乎有助于注释表现得更好:

library(dplyr)
library(gganimate)
library(ggplot2)

df <- read.csv("https://pastebin.com/raw/QvhdVqwM", stringsAsFactors = FALSE) %>%
mutate(date = as.Date(date))

# Dates are in descending order
df %>%
filter(country_code == "de") %>%
head %>%
as_tibble()

#> # A tibble: 6 x 10
#> big_change change_from_pre… date type region_id value city_name
#> <lgl> <int> <date> <chr> <chr> <int> <chr>
#> 1 FALSE -3 2020-05-28 one_… de-berlin 28 Berlin
#> 2 FALSE 3 2020-05-28 one_… de-hambu… 32 Hamburg
#> 3 FALSE 2 2020-05-28 one_… de-rhine… 31 Rhine-Ru…
#> 4 FALSE 2 2020-05-27 one_… de-berlin 32 Berlin
#> 5 FALSE -3 2020-05-27 one_… de-hambu… 28 Hamburg
#> 6 FALSE 3 2020-05-27 one_… de-rhine… 28 Rhine-Ru…
#> # … with 3 more variables: country_code <chr>, note <chr>, country <chr>

countries_anim <- df %>%
filter(country_code == "de") %>%
arrange(date) %>% # arranging by date solves the problem.
ggplot(aes(date, value, colour = city_name)) +
geom_line() +
geom_segment(aes(xend = max(date) - 30, yend = value), linetype = 2,
colour = "grey") +
geom_text(aes(x = max(date) - 29, label = city_name), hjust = 0) +
theme(legend.position = "bottom") +
guides(colour = guide_legend(title.position = "top")) +
transition_reveal(date)

country_anim <- animate(plot = countries_anim, duration = 10,
renderer = gifski_renderer(file = 'figures/countries.gif'))

我不太清楚为什么会发生这种情况,因为数据顺序并没有真正扰乱 gpplot2

enter image description here

关于r - gganimate 中的 Ghost geom_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62085854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com