gganimate 中的 Ghost geom_text
Ghost geom_text in gganimate
我正在玩 gganimate
,我相信它在标签方面有点古怪(我基本上遵循了 this 示例)。
我正在使用此代码片段生成以下 .gif(您可以找到数据 here,不希望 post 长度爆炸)。
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'))
除了一个小烦恼外,一切都很好:在动画的最开始,一些注释(应该遵循时间序列趋势)永久打印在绘图区域中。我尝试更改渲染器,但问题似乎完全不相关。
我不太精通 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
。
我正在玩 gganimate
,我相信它在标签方面有点古怪(我基本上遵循了 this 示例)。
我正在使用此代码片段生成以下 .gif(您可以找到数据 here,不希望 post 长度爆炸)。
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'))
除了一个小烦恼外,一切都很好:在动画的最开始,一些注释(应该遵循时间序列趋势)永久打印在绘图区域中。我尝试更改渲染器,但问题似乎完全不相关。
我不太精通 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
。