我正在嘗試在 R 中為直方圖設定影片。我創建了以下資料集:
library(ggplot2)
library(gganimate)
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 2
d = data.frame(a,b,i)
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 3
e = data.frame(a,b,i)
#data
g = rbind(c,d,e)
我能夠在基礎 R 和 ggplot 中制作靜態直方圖:
myplot1 = plot(g$a, type = "h")
myplot2 = ggplot(g, aes(x=a))
geom_histogram(binwidth=1)

問題是當我嘗試為這些圖形設定影片時(這些是 3 個圖形:i = 1,2,3):
#first attempt
animateplot <- myplot1 transition_time(i)
animateplot
NULL
#second attempt
anim <- myplot2 transition_manual(g$i)
ease_aes("linear")
enter_fade()
exit_fade()
anim
NULL
有人可以告訴我如何為這些圖形設定影片并將影片保存為 gif 或 html 檔案嗎?
謝謝!
uj5u.com熱心網友回復:
ggplot(g, aes(x=a))
geom_histogram(binwidth=1)
transition_states(i)

或者使用更多選項的變體:
# alternative fake data
g <- data.frame(a = c(rnorm(500,10,5), rnorm(2000, 20, 30), rnorm(180, 50, 2)),
i = c(rep(1, 500), rep(2, 2000), rep(3, 180)))
animate(
ggplot(g, aes(x=a))
geom_histogram(binwidth=1)
transition_states(i, state_length = 0.2)
labs(title = "Group: {closest_state}"),
fps = 25)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/315333.html
