使用 gganimate 渲染动画图时出现问题
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(国家、年份、比率) %>% filter(国家== "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)
我正在使用 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(国家、年份、比率) %>% filter(国家== "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)