据我所知,gganimate已在1.0.3版中发布,我们可以使用transition_*函数绘制动态图。但是当我运行以下代码时,出现错误:

Error in `$<-.data.frame`(`*tmp*`, "group", value = "") :
  replacement has 1 row, data has 0


码:

library(ggmap)
library(gganimate)
world <- map_data("world")
world <- world[world$region!="Antarctica",]
data <- data.frame(state = c("Alabama","Alaska","Alberta","Alberta","Arizona"),
                   lon = c(-86.55,-149.52,-114.05,-113.25,-112.05),
                   lat = c(33.30,61.13,51.05,53.34,33.30)
                   )
ggplot()+
  geom_map(data = world,
           map = world,
           aes(long,lat,map_id = region),
           color = '#333300',
           fill = '#663300') +
  geom_point(data = data,
             aes(x = lon, y = lat),
             size = 2.5) +
  geom_jitter(width = 0.1) +
  transition_states(states = state)

最佳答案

您没有在顶级ggplot()行中定义任何数据,因此state中的transition_*无处不在。

我也不清楚为什么您的代码中具有geom_jitter级别。像transition_*一样,它没有要继承的顶级数据/美学映射,因此,如果transition_*没有首先触发错误,它也将引发错误。另外,即使我们添加了映射,在给定数据中纬度/经度坐标的范围的情况下,抖动0.1几乎不会对视觉产生影响。

您可以尝试以下方法:

# put data in top level ggplot()
ggplot(data,
       aes(x = lon, y = lat))+
  geom_map(data = world,
           map = world,
           aes(long,lat,map_id = region),
           color = '#333300', fill = '#663300',
           # lighter background for better visibility
           alpha = 0.5) +
  geom_point(size = 2.5) +
  # limit coordinates to relevant range
  coord_quickmap(x = c(-180, -50), y = c(25, 85)) +
  transition_states(states = state)


r - 如何使用gganimate包在R中绘制动态 map ?-LMLPHP

关于r - 如何使用gganimate包在R中绘制动态 map ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56024713/

10-12 20:16