结合geom_point和geom_sf时,gganimate不会渲染

时间:2018-11-12 16:59:11

标签: r ggplot2 sf gganimate

我正在尝试以gganimate动画化在地图上移动的点。在下面的示例中,仅对点进行动画处理,并对点和地图进行静态绘图,但是将它们组合会失败,并显示错误Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) : zero-length inputs cannot be mixed with those of non-zero length

这是一个复制品:

加载库

# gganimate isn't on CRAN
devtools::install_github('thomasp85/gganimate')
library(tidyverse)
library(gganimate)
library(sf)
# for the spatial data
library(rnaturalearth)             

创建数据

# Points data
time <- seq(ISOdate(2015, 6, 1), ISOdate(2015, 8, 1), length.out = 100)
track1 <- tibble(lon = seq(-161, -155, length.out = 100),
                 lat = seq(19, 25, length.out = 100),
                 time = time,
                 trackid = 1)
track2 <- tibble(lon = seq(-155, -161, length.out = 100),
                 lat = seq(19, 25, length.out = 100),
                 time = time,
                 trackid = 2)
d <- rbind(track1, track2)

# Spatial data
earth <- st_as_sf(ne_download(scale = "medium",
                              category = "physical",
                              type = "coastline"))
deg_buff <- 1
lon_range <- range(d$lon) + c(-deg_buff, deg_buff)
lat_range <- range(d$lat) + c(-deg_buff, deg_buff)
bbox <- st_polygon(list(cbind(lon_range[c(1,1,2,2,1)], 
                              lat_range[c(1,2,2,1,1)])))
bbox <- st_sfc(bbox)
st_crs(bbox) <- st_crs(earth)
area <- st_intersection(earth, bbox)

动画点(有效)

p <- ggplot(d, aes(lon, lat)) +
  geom_point() +
  labs(subtitle = 'Date: {format(frame_time, "%b %e")}') +
  transition_components(trackid, time) +
  shadow_trail(distance = 0.01, size = 0.3)
animate(p, 100, 20)

绘制静态地图(有效)

ggplot(d, aes(lon, lat)) +
  geom_sf(data = area, inherit.aes = FALSE) +
  geom_point()

在背景中使用静态地图对点进行动画处理(失败)

p2 <- ggplot(d, aes(lon, lat)) +
  geom_sf(data = area, inherit.aes = FALSE) +
  geom_point() +
  labs(subtitle = 'Date: {format(frame_time, "%b %e")}') +
  transition_components(trackid, time) +
  shadow_trail(distance = 0.01, size = 0.3)
animate(p2, 100, 20)

2 个答案:

答案 0 :(得分:2)

1

  1. data = daes()ggplot()移至geom_point()
  2. transition_components()更改为transition_time()
  3. shadow_trail更改为shadow_wake
  4. (添加颜色)

代码

p2 <- ggplot() +
    geom_sf(data = area, color = "red") +
    geom_point(data = d, aes(lon, lat), inherit.aes = FALSE) +
    labs(subtitle = 'Date: {format(frame_time, "%b %e")}') +
    transition_time(time) +
    shadow_wake(0.3)

animate(p2, 100)

答案 1 :(得分:0)

trackid 是不确定的,在p2中引发错误; 时间已定义。