使用gganimate渲染动画情节时出现的问题

问题描述 投票:0回答:1

我正在使用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

enter image description here

仅过滤到巴西:

enter image description here

dput(data_rates_plot%>%select(国家,年份,比率)%>%filter(国家==“巴西”):]

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")

任何人都知道发生了什么事吗?

r ggplot2 data-visualization gif gganimate
1个回答
0
投票

这似乎与transition_reveal如何假设数据已排序有关。使用提供的数据我得到了相同的错误结果,但是当按Year排序时,结果消失了。

未分类的结果:

enter image description here

然后,使用排序:(并添加一个引入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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.