Black Lives Matter. Please consider donating to Black Girls Code today.

Issue adding colour aesthetic with ggplotly (when added all points appear on all frames)

I can generate the desired ggplotly plot with the x and y aesthetics and animate using f variable for the frame. However, when I add the colour aesthetic (z) all points for all frames appear (please see plots and code below).

test dataframe:

dftest <- data.frame(
  y = c(1,10,20,50,60,1,10,20,30,45,1,10,20,40,50,1,15,25,55,60,5,15,25,35,45,5,15,25,45,50,3,6,9,18,21,1,13,16,18,30,5,15,16,18,20), 
  x = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3), 
  z = c(20,18,16,14,12,18,17,16,14,10,18,16,14,12,8,15,14,12,8,6,18,17,16,14,10,18,16,14,12,8,20,18,16,14,12,18,17,16,14,10,18,16,14,12,8),
  f = c("a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","b","b","b","b","b","b","b","b","b","c","c","c","c","c","c","c","c","c","c","c","c","c","c","c")
)

First ggplotly graph without colour, which works perfectly:

p1 <- ggplot(dftest, aes(x = factor(x), y = y))+
      geom_point(aes(size = 8, frame = f))+
      theme_bw()
ggplotly(p1)

Then to add colour - note this was tried in both the ggplot and geom_point aesthetics

p2 <- ggplot(dftest, aes(x = factor(x), y = y, colour =z))+
      geom_point(aes(size = 8, frame = f))+
      theme_bw()
ggplotly(p2)

However, this gives all points of all frames on all frames of the plot. Note also the frame information/labels are lost.

Thanks, Philippa

Turns out this works using R 4.0.0 but not with 3.6.3.