R ggplot中的动画地图

Animate Map in R ggplot

我正在使用 R 和库 gganimate,并且有一个多边形数据框和一个点数据框:

这是多边形数据框(poly):

 long    lat order hole  piece    id group T_P_0_14 T_P_15_64 T_P_65mas  P_TOT P_TOT_HOM
   <dbl>  <dbl> <int> <lgl> <fct> <dbl> <fct>    <dbl>     <dbl>     <dbl>  <dbl>     <dbl>
1 5.80e5 6.14e6  2883 FALSE 1        10 10.1     25523    122781     38716 187020     83348
2 5.80e5 6.14e6  2883 FALSE 1        10 10.1     25523    122781     38716 187020     83348
3 5.80e5 6.14e6  2883 FALSE 1        10 10.1     25523    122781     38716 187020     83348
4 5.80e5 6.14e6  2883 FALSE 1        10 10.1     25523    122781     38716 187020     83348
5 5.80e5 6.14e6  2883 FALSE 1        10 10.1     25523    122781     38716 187020     83348
6 5.80e5 6.14e6  2883 FALSE 1        10 10.1     25523    122781     38716 187020     83348
7 5.80e5 6.14e6  2883 FALSE 1        10 10.1     25523    122781     38716 187020     83348
8 5.80e5 6.14e6  2884 FALSE 1        10 10.1     25523    122781     38716 187020     83348
9 5.80e5 6.14e6  2884 FALSE 1        10 10.1     25523    122781     38716 187020     83348

这是点数据框(点):

   codigo  anio   month ascensos coords.x1 coords.x2
   <int> <dbl> <dbl>    <int>     <dbl>     <dbl>
1    546  2013     1      489   578024.  6140711.
2    546  2013     2      403   578024.  6140711.
3    546  2013     3      504   578024.  6140711.
4    546  2013     4      556   578024.  6140711.
5    546  2013     5      505   578024.  6140711.
6    546  2013     6      481   578024.  6140711.
7    546  2013     7      477   578024.  6140711.
8    546  2013     8      512   578024.  6140711.
9    546  2013     9      459   578024.  6140711.

我正在尝试使用 gganimate 制作动画图,使用月份作为过渡变量,因为我想查看点值在几个月内随时间的演变。然而,动画显示了点的移动,这是不可能的,因为它们是固定的。相反,我想显示这些点的颜色变化,这取决于变量 ascensos。

我该如何解决这个问题?有什么想法吗?

非常感谢

ggplot() +
 geom_polygon(data = poly %>% mutate(Macrozona=as.factor(id)),
              aes(x = long, y = lat, group = group),
              colour = "black")+
 geom_point(aes(x=coords.x1, y=coords.x2, color=ascensos), data=points %>% filter(anio==2019), 
               alpha=0.1)+
 scale_color_gradient(low="blue", high="red")+
 labs(subtitle = paste('Month: {frame_time}'))+
 transition_time(month)

您 运行 遇到的麻烦可能是月份变量的格式。 transition_time() 要求数据采用时间格式,但您的数据框显示 monthdbl 格式。

请尝试使用 transition_states(),因为该命令可以更灵活地使用数据。 transition_states() 还将根据需要使多边形图层保持静态。