R ggplot2和ggplotly在地图上一年中的累计点 [英] Cumulative points over year on map with R ggplot2 and ggplotly

查看:52
本文介绍了R ggplot2和ggplotly在地图上一年中的累计点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to plot new locations opened over each month on a map cumulatively. I am able to create an animation with new locations each month, but not cumulatively. In other words, I want to see the new locations add to the existing ones.

这是示例数据

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, cumulative=TRUE,
                 frame=month,stat = 'identity' ),data = DF )
map

# Generate the Visual and a HTML output
ggp <- ggplotly(map)%>%
  animation_opts(transition = 0)
ggp

输出未累计显示位置.我想基本上看到所有四个位置.

The output does not show locations cumulatively. I want to see all four locations in the end basically.

推荐答案

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

If you use gganimate you can include transition_states to animate your points. For cumulative addition of points, use shadow_mark to include data behind the current frame.

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 .

Edit: To save the animation as a .gif, use anim_save.

anim_save("mapanim.gif", map)

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

In addition, if you want to change the width/height of the final animation, you can specify, for example:

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

这篇关于R ggplot2和ggplotly在地图上一年中的累计点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆