使用 gganimate 在两个时间序列条形图之间创建平滑过渡
Creating a smooth transition between two time series bar charts with gganimate
我正在尝试用 gganimate 制作一个动画,以过去 10 年的犯罪数据开始。动画开始后,我想添加可追溯到 1970 年的历史年份(当时犯罪率高得多)。这将显示为带有自由轴的 "zoom out"。
到目前为止,我可以从 2018 年开始,像这样依次向后添加年份:
# Create dataset
dat <- tibble(year = 1970:2018)
dat$crime <- 100 * exp(-0.02*(dat$year-1970))
p <- ggplot(dat, aes(x=year, y = crime)) +
geom_bar(stat = "identity") +
transition_states(-year, transition_length = 4, state_length = 2) +
view_follow() + shadow_mark()
animate(p)
在缩小到过去 30-40 年之前,我很难从 10 年的历史情节(而不是仅仅一年)开始。如有任何帮助,我们将不胜感激!
使用自定义 state
变量对所需年份进行分组。
数据
dat <- tibble(year = 1970:2018)
dat$crime <- 100 * exp(-0.02*(dat$year-1970))
# state variable called "time" for grouping
dat$time <- c(40:2, rep(1, 10))
代码
p <- ggplot(dat) +
geom_col(
aes(
x = year,
y = crime
)
) +
# states depend on "time", not "year"
transition_states(
time,
transition_length = 4,
state_length = 2
) +
view_follow() +
shadow_mark()
animate(p)
PS:这是一个非常简洁、格式良好且可重现的第一个问题!继续加油!
我正在尝试用 gganimate 制作一个动画,以过去 10 年的犯罪数据开始。动画开始后,我想添加可追溯到 1970 年的历史年份(当时犯罪率高得多)。这将显示为带有自由轴的 "zoom out"。 到目前为止,我可以从 2018 年开始,像这样依次向后添加年份:
# Create dataset
dat <- tibble(year = 1970:2018)
dat$crime <- 100 * exp(-0.02*(dat$year-1970))
p <- ggplot(dat, aes(x=year, y = crime)) +
geom_bar(stat = "identity") +
transition_states(-year, transition_length = 4, state_length = 2) +
view_follow() + shadow_mark()
animate(p)
在缩小到过去 30-40 年之前,我很难从 10 年的历史情节(而不是仅仅一年)开始。如有任何帮助,我们将不胜感激!
使用自定义 state
变量对所需年份进行分组。
数据
dat <- tibble(year = 1970:2018)
dat$crime <- 100 * exp(-0.02*(dat$year-1970))
# state variable called "time" for grouping
dat$time <- c(40:2, rep(1, 10))
代码
p <- ggplot(dat) +
geom_col(
aes(
x = year,
y = crime
)
) +
# states depend on "time", not "year"
transition_states(
time,
transition_length = 4,
state_length = 2
) +
view_follow() +
shadow_mark()
animate(p)
PS:这是一个非常简洁、格式良好且可重现的第一个问题!继续加油!