3

As I know gganimate has been in version 1.0.3,we can use transition_* function to plot a dynamic graph. But when I run following code ,there is an error :

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

code:

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)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Tao Hu
  • 287
  • 2
  • 12

1 Answers1

2

You have no data defined in the top level ggplot() line, so state in transition_* comes out of nowhere.

It's also unclear to me why you have a geom_jitter level in your code. Like transition_*, it has no top level data / aesthetic mappings to inherit, so it would have thrown an error too, had transition_* not triggered an error first. In addition, even if we add the mappings, given the range of lat/lon coordinates in your data, jittering by 0.1 is hardly going to matter visually.

You can try the following:

# 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)

plot

Z.Lin
  • 28,055
  • 6
  • 54
  • 94