我正在尝试使用plotly绘制动画帧。目的是绘制两个连续变量和一个分类变量中某些单位(id)的变化方式。出现问题是因为第三类不是从一开始就出现的。在该图中,具有“ type3”类别的单位不会在2001年之后出现在图中,并且“ type3”条目也不会出现在图例中。该代码显示了数据结构和问题的示例。
这个问题有解决方案吗?是否可以从图例的开始就包括所有3个类别?
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')