在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()还将根据您的需要保持多边形层的静态。

最新更新