使用 R ggplot2 和 ggplotly 在地图上累计点数
Cumulative points over year on map with R ggplot2 and ggplotly
我正在尝试在地图上累积地绘制每个月打开的新位置。我可以每个月创建一个带有新位置的动画,但不能累积。换句话说,我希望看到新位置添加到现有位置。
这是示例数据
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
输出不累积显示位置。我想基本上把四个地方都看完
如果您使用 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)
另外,如果要改变最终动画的width/height,可以指定,例如:
animate(map, height = 400, width = 600)
我正在尝试在地图上累积地绘制每个月打开的新位置。我可以每个月创建一个带有新位置的动画,但不能累积。换句话说,我希望看到新位置添加到现有位置。
这是示例数据
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
输出不累积显示位置。我想基本上把四个地方都看完
如果您使用 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)
另外,如果要改变最终动画的width/height,可以指定,例如:
animate(map, height = 400, width = 600)