[英]gganimate: How to make stacked bar chart grow smoothly upwards from x-axis
我在為堆疊的 static plot 設置動畫時遇到問題。 (見下文 animation)
animation 從左到右以不穩定的方式移動,而不是每個條從 x 軸向上增長。 堆疊的部分也不會很順利地一起生長,我似乎無法解決。
我需要在我的代碼中進行哪些更改,以便隨着動畫 plot 沿 x 軸過渡,各個條形向上增長? 我還想讓堆疊的條更平滑地增長,所以堆疊的條部分不會“扔”在彼此的頂部。
這怎么可能?
本文頂部的 gif 是我正在尋找的,但我不明白他的代碼與我的代碼有何不同以及在哪里:理想 animation
這是我的代表:
#Static df
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
year <- as.numeric(c(1996:2015,
1996:2015,
1996:2015))
c <- c(39, 40, 67, 80, 30, 140, 90, 23, 100, 123,
140, 160, 100, 89, 173, 200, 32, 90, 100, 211,
1, 2, 1, 13, 3, 3, 30, 1, 3, 3,
1, 1, 20, 2, 1, 10, 1, 2, 1, 1,
1, 3, 1, 2, 1, 3, 1, 3, 6, 1,
1, 30, 1, 1, 8, 9, 1, 32, 1, 1)
cat <- as.character(c("out", "out", "out", "out", "out", "out", "out", "out", "out", "out",
"out", "out", "out", "out", "out", "out", "out", "out", "out", "out",
"in", "in", "in", "in", "in", "out", "in", "in", "in", "in",
"in", "in", "in", "in", "in", "in", "in", "in", "in", "in",
"other", "other", "other", "other", "other", "other", "other", "other", "other", "other",
"other", "other", "other", "other", "other", "other", "other", "other", "other", "other"))
cat <- as.factor(cat)
static_df_test <- data.frame(year, c, cat) %>%
select(year, cat, c) %>%
arrange(year, cat)
#Static plot
library(ggplot2)
(static_plot <- ggplot(static_df_test) +
geom_bar(data = static_df_test, stat="identity", position ="stack",
aes(x = year, y = c, fill = cat)) +
scale_x_continuous(breaks = seq(1996, 2015, 1),
expand = c(0.003, 0.003),
labels = c(1996, 97, 98, 99, 2000,
"01", "02", "03", "04", "05", "06",
"07", "08", "09", 10, 11, 12, 13, 14, 15)) +
scale_y_continuous(breaks = seq(0, 250, 25),
expand = c(0,0),
limits = c(0,250)) +
labs(x = "year", y = "c")
)
#Animated plot
library(gganimate)
library(ggplot2)
ani <- (static_plot) +
transition_time(as.integer(year)) +
enter_grow() + shadow_mark(past = TRUE)
animate(ani, fps = 8, 50, duration = 15,
width = 1500, height = 750, renderer = gifski_renderer())
現在,條形圖通過從上一個條形向右移動來過渡。 在此處查看當前的 animation:
預先感謝您的任何幫助!
可能有更正確的方法可以做到這一點,但可以通過更改構建初始 static plot 的方式並使用transition_layers()
和enter_drift()
來完成。
請注意,為了簡化我的示例,我只包括 1996-2000 年。 要為所有年份設置動畫,只需復制並粘貼geom_bar()
部分並更改被過濾的年份。
#Static plot
library(ggplot2)
(static_plot <- ggplot(static_df_test) +
geom_bar(data = static_df_test %>% filter(year == 1996), stat="identity", position ="stack",
aes(x = year, y = c, fill = cat)) +
geom_bar(data = static_df_test %>% filter(year == 1997), stat="identity", position ="stack",
aes(x = year, y = c, fill = cat)) +
geom_bar(data = static_df_test %>% filter(year == 1998), stat="identity", position ="stack",
aes(x = year, y = c, fill = cat)) +
geom_bar(data = static_df_test %>% filter(year == 1999), stat="identity", position ="stack",
aes(x = year, y = c, fill = cat)) +
geom_bar(data = static_df_test %>% filter(year == 2000), stat="identity", position ="stack",
aes(x = year, y = c, fill = cat)) +
scale_x_continuous(breaks = seq(1996, 2015, 1),
expand = c(0.003, 0.003),
labels = c(1996, 97, 98, 99, 2000,
"01", "02", "03", "04", "05", "06",
"07", "08", "09", 10, 11, 12, 13, 14, 15)) +
scale_y_continuous(breaks = seq(0, 250, 25),
expand = c(0,0),
limits = c(0,250)) +
labs(x = "year", y = "c")
)
#Animated plot
library(gganimate)
library(ggplot2)
ani <- (static_plot) +
transition_layers(layer_length = 1, transition_length = 2) +
enter_drift(x_mod = 0, y_mod = -max(static_df_test$c))
animate(ani, fps = 10, duration = 10,
width = 600, height = 500, renderer = gifski_renderer())
通過將每個基於年份的條形圖放置在其自己的圖層上,我們可以使用transition_layers()
使它們一一出現,同時保留所有舊圖層。 我們使用enter_drift()
為每個條設置動畫,就好像它從圖表下方向上漂移一樣。
動畫 output 在這里:鏈接
有一個更簡單的解決方案。 將您的“年份”變量視為一個因素而不是數字。 這將使酒吧像你想要的那樣平穩地向上增長。
基本上為您的 static plot 代碼使用以下塊,它將起作用:
static_plot <- ggplot(static_df_test) +
geom_bar(data = static_df_test, stat="identity", position ="stack",
aes(x = as.factor(year), y = c, fill = cat)) +
scale_y_continuous(breaks = seq(0, 250, 25),
expand = c(0,0),
limits = c(0,250)) +
labs(x = "year", y = "c")
我知道我有點晚了,但我希望它有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.