Black Lives Matter. Please consider donating to Black Girls Code today.
https://www.blackgirlscode.com

New color group appearing not from the first frame

I am trying to plot an animated frame with plotly. The goal is to plot how some units (id) changes in two continuous variables AND one categorical. The problem arises because the third category appears not from the beginning. In the plot, the units with “type3” category do not appear on the plot after 2001 and the ‘type3’ entry does not appear in the legend. The code shows an example of the data structure and problem. The same problem with boxplot - only two categories are present for all frames.

df <- data.frame(x = rnorm(50), 
                 y = rnorm(50), 
                 year = c(rep(2000, 10), rep(2001, 10), rep(2002, 10), rep(2003, 10), rep(2004, 10)),
                 id = rep(1:10, 5),
                 model = c(rep('type1', 5), rep('type2', 5), rep('type1', 5) , rep('type2', 5),
                           rep('type1', 3), rep('type2', 2), rep('type3', 5) ,
                           rep('type1', 4), rep('type2', 5), rep('type3', 1) ,
                           rep('type1', 3), rep('type2', 2), rep('type3', 5) ))

cols <- c(
  "type1" = "#bc80bd",
  
  "type2" = "#fdb462", 
  "type3" = "#b3de69"
)



plot_ly(df,  x = ~x, 
        y= ~y, 
        color = ~model,
        text = ~id,
        frame = ~year,
        opacity = .8, 
        marker = list(size = 10),
        colors = cols,
        type = 'scatter')

plot_ly(df, 
        y= ~y, 
        color = ~model,
        frame = ~year,
        opacity = .8, 
        colors = cols,
        type = 'box')

Is there any solution to this problem? Is it possible to include all 3 categories from the beginning in the legend?