# Unwanted duplicate points ggplotly animation

**URL:** <https://community.plotly.com/t/unwanted-duplicate-points-ggplotly-animation/21862>\
**Category:** Plotly R\
**Created:** [April 3, 2019, 7:29pm UTC](https://community.plotly.com/t/unwanted-duplicate-points-ggplotly-animation/21862 "2019-04-03T19:29:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cramjaco](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@cramjaco](https://community.plotly.com/u/cramjaco)\
**Post date:** [April 3, 2019, 7:29pm UTC](https://community.plotly.com/t/unwanted-duplicate-points-ggplotly-animation/21862/1 "2019-04-03T19:29:12Z")

</div>

Lets say I have four groups of numbers (groups 1-4) that have some x (1-10), y (1-10) and time (1-10 coordinates. However, groups 1 and 2 stop existing after time-point 5.

```auto
pt <- data.frame(
  xx = rep(1:10, 10),
  yy = jitter(rep(1:10, 10)),
  tt = rep(1:10, each = 40),
  gg = rep(1:4, each = 10)
) %>%
  filter(!(gg %in% c(1,2) & tt > 5)) %>%
  mutate(yy = yy + gg)

```

I create an animated plot, color coding the points by the group gg and animating by value tt

```auto
pp <- pt %>% ggplot(aes(x = xx, y = yy, fill = as.factor(gg), frame = tt, group = gg)) +
  geom_point(shape = 21, colour = "black", size = 2) +
  scale_fill_viridis_d()

  ppp <- ggplotly(pp)
  
  ppp

```

This gives me a plot that behaves as expected for time points 1-5

 ![timepoint5](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/6/6bc2127295431375287acfa58212a7e19e4ac828.png)

However, when the plot advances to time-point 6 two related strange things happen.  
(1) The points from groups 1 and 2 move into the position into the position of the points from groups 3 and 4.  
(2) The points that were originally from groups 3 and 4 remain frozen in their final positions. I am left with something that looks like this. With two points per group, only one of which moves correctly and the other of which is frozen in the position from time point 5.

 ![timepoint6](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/2/2af43d57f098a7922df04d5b17c004174fec7fa2.png)

If, instead of treating gg as a factor `fill = as.factor(gg)` when I set up the plot, I instead leave it as a number `fill = gg` I get the afformetntioned behavior (1) but not behavior (2). That is the points shuffle positions in a way that I do not want, but at least the old groups don’t hang around confusing the viewer.

Any suggestions about how I can resolve these two plotting issues? Thanks.

---

<div class="post-metadata">

**Author:** ![cramjaco](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@cramjaco](https://community.plotly.com/u/cramjaco)\
**Post date:** [April 3, 2019, 8:06pm UTC](https://community.plotly.com/t/unwanted-duplicate-points-ggplotly-animation/21862/2 "2019-04-03T20:06:15Z")

</div>

I discovered that I can solve problem (2), with even with the factors, if I specify `animation_opts(redraw = TRUE)`.

So now my call is

```auto
pp <- pt %>% ggplot(aes(x = xx, y = yy, fill = as.factor(gg), frame = tt, group = gg)) +
  geom_point(shape = 21, colour = "black", size = 2) +
  scale_fill_viridis_d()

  ppp <- ggplotly(pp) %>% animation_opts(redraw = TRUE)
  
  ppp

```

However, problem (1) where the points from groups 1 and 2 jump into the position of points from groups 3 and 4 (and then replace those points), rather than just disappearing persists. I’d still appreciate help on that front.
