[英]Issue when rendering an animated plot using gganimate
我在 R 中使用gganimate
包來為我的情節設置動畫。 它運行良好,但標志重復並且前三個圖像也卡住了。
data_rates_plot %>%
ggplot(aes(x = Year, y = Rate, group = factor(Country))) +
geom_line(aes(color = factor(Country))) +
geom_image(aes(image=Image)) +
transition_reveal(Year) +
scale_color_manual(values=c("#34ba5c", "#000000", "#db0000", "#050c75")) +
theme(element_line('black'),
axis.line = element_line(color = "black", size = 0.5, linetype = "solid"),
plot.background = element_rect(fill = '#fff7e6'), panel.background = element_rect(fill = '#fff7e6'),
panel.grid = element_line(colour = NULL, linetype = 3),
panel.grid.major = element_line(colour = "black"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank() ,plot.title = element_text(hjust = 0, face = "bold"),
plot.margin = unit(c(1, 1, 1, 1), "lines"),
strip.background = element_rect(),
legend.position = "none") +
labs(title = "Ratio de homicidios (1990-2017)", x = "Año", y = "Ratio (núm. homicidios/100.000 hab.)")
這是我用來繪制的數據集的樣本:
> head(data_rates_plot)
Country Year Rate Continent Image
1 Brazil 1994 28.59261 Americas https://upload.wikimedia.org/wikipedia/commons/0/01/Brazil_flag_300.png
2 Brazil 2003 32.46278 Americas https://upload.wikimedia.org/wikipedia/commons/0/01/Brazil_flag_300.png
3 Brazil 2015 30.32380 Americas https://upload.wikimedia.org/wikipedia/commons/0/01/Brazil_flag_300.png
4 Brazil 1991 26.23049 Americas https://upload.wikimedia.org/wikipedia/commons/0/01/Brazil_flag_300.png
5 Brazil 1993 27.26052 Americas https://upload.wikimedia.org/wikipedia/commons/0/01/Brazil_flag_300.png
6 Brazil 2000 32.13900 Americas https://upload.wikimedia.org/wikipedia/commons/0/01/Brazil_flag_300.png
過濾到僅巴西:
dput(data_rates_plot %>% select(Country, Year, Rate) %>% filter(Country == "Brazil")):
structure(list(Country = c("Brazil", "Brazil", "Brazil", "Brazil",
"Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil",
"Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil",
"Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil",
"Brazil", "Brazil", "Brazil"), Year = c(1994L, 2003L, 2015L,
1991L, 1993L, 2000L, 2017L, 1990L, 2002L, 1997L, 2012L, 1995L,
1992L, 2011L, 2004L, 2001L, 2013L, 1996L, 2006L, 2009L, 2014L,
1999L, 2005L, 2010L, 2016L, 1998L, 2007L, 2008L), Rate = c(28.5926053974604,
32.4627790546635, 30.3237998934026, 26.2304907371429, 27.2605158574606,
32.1390025655279, 30.1328892734601, 26.7440815134982, 32.7898675004014,
31.1361250032559, 30.6407022936813, 30.4476309648573, 26.0500124795172,
30.2078811223668, 31.6613393520385, 32.4090823805319, 30.7139681276347,
30.5758007548371, 30.5130381597837, 30.0623192371181, 30.686318995567,
31.679684049261, 30.7334658622574, 30.145587333775, 30.5099622110466,
31.5382871119106, 30.1461623438502, 29.9688608949252)), row.names = c(NA,
-28L), class = "data.frame")
有誰知道發生了什么?
這看起來像是transition_reveal
如何假設數據已排序的問題。 我使用提供的數據得到了同樣的錯誤結果,但是當我按年份排序時,這種情況就消失了。
未排序結果:
然后,使用排序:(並添加一個步驟來引入 URL)
data_rates_plot %>%
mutate(Image = "https://upload.wikimedia.org/wikipedia/commons/0/01/Brazil_flag_300.png") %>%
arrange(Year) %>%
ggplot(aes(x = Year, y = Rate, group = factor(Country))) +
... [same as OP)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.