R ggplot2和ggplotly在地图上一年中的累计点

时间:2019-08-30 23:46:56

标签: r ggplot2 ggplotly

我正在尝试在地图上累计绘制每个月开放的新位置。我每个月都可以创建一个具有新位置的动画,但是不能累积。换句话说,我希望看到新位置添加到现有位置。

这是示例数据

systemctl stop backend.service
systemctl freeze backend.service  # ?

这是我尝试过的

DF <- data.frame("latitude" = c(42.29813,41.83280,41.83280,30.24354),
                 "longitude" =c(-71.23154,-72.72642,-72.72642,-81.62098),
                 "month" = c(1,2,3,4))

输出未累计显示位置。我基本上希望最后看到所有四个位置。

1 个答案:

答案 0 :(得分:1)

如果使用gganimate,则可以包含transition_states来为点设置动画。要累积点数,请使用shadow_mark将数据包括在当前帧的后面。

library(ggthemes)
library(gganimate)
library(ggplot2)

DF <- data.frame("latitude" = c(42.29813,41.83280,41.83280,30.24354),
                 "longitude" =c(-71.23154,-72.72642,-72.72642,-81.62098),
                 "month" = c(1,2,3,4))

usa <- ggplot() +
  borders("usa", colour = "gray85", fill = "gray80") +
  theme_map() 

map <- usa +
  geom_point(aes(x = longitude, y = latitude), color = "black", data = DF) +
  transition_states(month, transition_length = 0, state_length = 1) + 
  shadow_mark()

map

编辑:要将动画另存为.gif,请使用anim_save

anim_save("mapanim.gif", map)

此外,如果要更改最终动画的宽度/高度,则可以指定,例如:

animate(map, height = 400, width = 600)

animation of cumulative points on map