我的目標是重現 Ed Hawkins 關于氣候變化的影片R人物gganimate。這個數字叫做
影片 GIF:

乍一看,結果看起來相同,但細節顯示差異(例如,標記的藍線)。

在這個包含合成資料的示例代碼中,差異很小。但是對于真實資料,由于許多紅線(最近的高溫資料點)在背景中消失了,因此這些數字看起來非常不同。那么,如何transition_reveal()按日期保留訂單?任何幫助表示贊賞,非常感謝!
uj5u.com熱心網友回復:
這本身不是答案。這就是為什么。鑒于此資訊,您必須告訴我您更喜歡什么,以便我為您提供解決方案。
我嘗試了一些東西——我確信每一個都可以作業,但沒有。所以,我想看看發生了什么ggplot。我的預感被證明是正確的。您的資料在 中的順序,value_yr而png不是year.
我在最后重復這個問題:
您可以按順序排列影片,也可以按年份排列
value_yr顏色。ggplot你更喜歡哪個?
我怎么知道?我提取了物件中指定的顏色。
tellMe <- ggplot_build(temp)$data[[1]]
head(tellMe)
# colour x y group PANEL flipped_aes size linetype alpha
# 1 #1E60A4 1 -1.75990067 1 1 FALSE 0.6 1 NA
# 2 #1E60A4 2 -0.08968196 1 1 FALSE 0.6 1 NA
# 3 #1E60A4 3 -0.69657130 1 1 FALSE 0.6 1 NA
# 4 #1E60A4 4 -0.10777727 1 1 FALSE 0.6 1 NA
# 5 #1E60A4 5 1.57710505 1 1 FALSE 0.6 1 NA
# 6 #1E60A4 6 1.63277369 1 1 FALSE 0.6 1 NA
gimme <- tellMe %>% group_by(group) %>%
summarise(color = unique(colour)) %>%
print(n = 100) # there are less than 100, I just want them all
head(gimme)
# # A tibble: 6 × 2
# group color
# <int> <chr>
# 1 1 #1E60A4
# 2 2 #114781
# 3 3 #175290
# 4 4 #053061
# 5 5 #1C5C9E
# 6 6 #3E8BBF
對我來說,這表明顏色不是按組順序排列的,所以我想查看顏色以可視化順序。
我使用了這個功能。我知道它來自一個演示,但我不記得是哪一個。我只是看了看,所以我可以在這里包含它,但我沒有找到它。
# this is from a demo (not sure which one anymore!
showCols <- function(cl=colors(), bg = "lightgrey",
cex = .75, rot = 20) {
m <- ceiling(sqrt(n <-length(cl)))
length(cl) <- m*m; cm <- matrix(cl, m)
require("grid")
grid.newpage(); vp <- viewport(w = .92, h = .92)
grid.rect(gp=gpar(fill=bg))
grid.text(cm, x = col(cm)/m, y = rev(row(cm))/m, rot = rot,
vp=vp, gp=gpar(cex = cex, col = cm))
}
showCols(gimme$color)
左上角的顏色是最舊的年份,它下面的值是第二年,依此類推。最近一年是最右側列中的底部值。

df %>% group_by(yr) %>% summarise(value_yr = unique(value_yr))
# they are in 'value_yr' order in ggplot, not year
# # A tibble: 71 × 2
# yr value_yr
# <int> <dbl>
# 1 1950 0.0380
# 2 1951 -0.215
# 3 1952 -0.101
# 4 1953 -0.459
# 5 1954 -0.00130
# 6 1955 0.559
# 7 1956 -0.457
# 8 1957 -0.251
# 9 1958 1.10
# 10 1959 0.282
# # … with 61 more rows
您可以按順序排列影片,也可以按年份排列
value_yr顏色。ggplot你更喜歡哪個?
更新
您不會使用transition_reveal同一元素進行分組和轉換。不幸的是,我不能告訴你為什么,但它似乎卡在 1958 年!
要使左側的此 gif 與ggplot右側的 png 匹配:


ggplot首先,我修改了對和的呼叫geom_line
ggplot(aes(x = month(date, label = T), y = value,
group = yr, color = yr))
geom_line(size = .6)
Then I tried to use transition_reveal but noticed that subsequent years were layered underneath other years. I can't account for that odd behavior. When I ran showCol after changing temp, the colors were in order. That ruled out what I had thought the problem was initially.
I modified the object anim, using transition_manual to force the order of the plot layers.
anim <- temp
transition_manual(yr, cumulative = T)
ease_aes('linear')
That's it. Now the layers match.
As to whether this would have worked before you changed the color assignment: original plot with manual transitions of the year on the left, ggplot png on the right:


It looks like that would've have worked, as well. So, my original drawn-out explanation wasn't nearly as useful as I thought, but at least you have a working solution now. (Sigh.)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450639.html
