有什么方法可以创建一个以范围作为过渡时间的动画(gganimate)?
is there any way to create an animation (gganimate) with ranges as transition times?
有什么方法可以让 gganimate 在数年范围内的过渡时间内工作?在我的数据中,我有三个时间点,其中两个是如下所示的范围。
数据:
Year rate group
2012-2014 7 Other CT, White
2015-2017 11 Other CT, White
2018 3 Fairfield, Black
2018 2 Fairfield, Hispanic
这是我想要制作动画的 ggplot 代码示例
data %>% ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {frame_time}") +
transition_time(Year)
当我将转换时间输入为 "Year" 时出现错误,因为我的 Year 变量是一个容纳范围的字符。这是我得到的错误:
Error: time data must either be integer, numeric, POSIXct, Date, difftime, orhms
我能做些什么来绕过这个错误并继续保持原来的范围吗?
我建议要么使用 transition_manual
并将年份视为类别(失去平滑过渡),要么将年份范围转换为数字。
library(tidyverse); library(gganimate)
df1 <- tribble(~Year, ~rate, ~group,
"2012-2014", 7, "grp1",
"2015-2017", 11, "grp1",
"2018", 3, "grp1")
第一种方法,将 Year 保持原样作为字符:
df1 %>%
ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {current_frame}") +
transition_manual(Year)
第二种方法,将年份转换为数字。在这种情况下,我只使用了第一年,但您也可以将该值分配给平均年份,或者添加具有范围内每一年值的行。
df1 %>%
mutate(Year_numeric = parse_number(Year)) %>%
ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {round(frame_time)}") +
transition_time(Year_numeric)
最后,如果您想表示给定级别的所有范围年份,您可以为所有组成年份创建行。但这需要一些苦力:
df1 %>%
# For ranged years, find how many in range:
mutate(year_num = 1 + if_else(Year %>% str_detect("-"),
str_sub(Year, start = 6) %>% as.numeric() -
str_sub(Year, end = 4) %>% as.numeric(),
0)) %>%
# ... and use that to make a row for each year in the range
tidyr::uncount(year_num) %>%
group_by(Year) %>%
mutate(Year2 = str_sub(Year, end = 4) %>% as.numeric() +
row_number() - 1) %>%
ungroup() %>%
# FYI at this point it looks like:
# A tibble: 7 x 4
# Year rate group Year2
# <chr> <dbl> <chr> <dbl>
#1 2012-2014 7 grp1 2012
#2 2012-2014 7 grp1 2013
#3 2012-2014 7 grp1 2014
#4 2015-2017 11 grp1 2015
#5 2015-2017 11 grp1 2016
#6 2015-2017 11 grp1 2017
#7 2018 3 grp1 2018
ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {round(frame_time)}") +
transition_time(Year2)
有什么方法可以让 gganimate 在数年范围内的过渡时间内工作?在我的数据中,我有三个时间点,其中两个是如下所示的范围。
数据:
Year rate group
2012-2014 7 Other CT, White
2015-2017 11 Other CT, White
2018 3 Fairfield, Black
2018 2 Fairfield, Hispanic
这是我想要制作动画的 ggplot 代码示例
data %>% ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {frame_time}") +
transition_time(Year)
当我将转换时间输入为 "Year" 时出现错误,因为我的 Year 变量是一个容纳范围的字符。这是我得到的错误:
Error: time data must either be integer, numeric, POSIXct, Date, difftime, orhms
我能做些什么来绕过这个错误并继续保持原来的范围吗?
我建议要么使用 transition_manual
并将年份视为类别(失去平滑过渡),要么将年份范围转换为数字。
library(tidyverse); library(gganimate)
df1 <- tribble(~Year, ~rate, ~group,
"2012-2014", 7, "grp1",
"2015-2017", 11, "grp1",
"2018", 3, "grp1")
第一种方法,将 Year 保持原样作为字符:
df1 %>%
ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {current_frame}") +
transition_manual(Year)
第二种方法,将年份转换为数字。在这种情况下,我只使用了第一年,但您也可以将该值分配给平均年份,或者添加具有范围内每一年值的行。
df1 %>%
mutate(Year_numeric = parse_number(Year)) %>%
ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {round(frame_time)}") +
transition_time(Year_numeric)
最后,如果您想表示给定级别的所有范围年份,您可以为所有组成年份创建行。但这需要一些苦力:
df1 %>%
# For ranged years, find how many in range:
mutate(year_num = 1 + if_else(Year %>% str_detect("-"),
str_sub(Year, start = 6) %>% as.numeric() -
str_sub(Year, end = 4) %>% as.numeric(),
0)) %>%
# ... and use that to make a row for each year in the range
tidyr::uncount(year_num) %>%
group_by(Year) %>%
mutate(Year2 = str_sub(Year, end = 4) %>% as.numeric() +
row_number() - 1) %>%
ungroup() %>%
# FYI at this point it looks like:
# A tibble: 7 x 4
# Year rate group Year2
# <chr> <dbl> <chr> <dbl>
#1 2012-2014 7 grp1 2012
#2 2012-2014 7 grp1 2013
#3 2012-2014 7 grp1 2014
#4 2015-2017 11 grp1 2015
#5 2015-2017 11 grp1 2016
#6 2015-2017 11 grp1 2017
#7 2018 3 grp1 2018
ggplot(aes(y = rate, x = group)) +
geom_col() +
coord_flip() +
labs(title = "Year: {round(frame_time)}") +
transition_time(Year2)