Interactively change color of points in traces in R plotly -
i want create interactive plotly 3d scatter markers , lines in r. graphic should able highlight individual traces, working. should able change color according other variables.
here example of want do, highlighting works fine, changing color not work:
library(plotly) irs <- data.table(iris) setkey(irs, `species`) p <- plot_ly(type = "scatter3d", mode = "lines+markers") (i in levels(irs$species)) { xx <- irs[i]$sepal.length yy <- irs[i]$sepal.width zz <- irs[i]$petal.length cc <- irs[i]$petal.width p <- p %>% add_trace(x = xx, y = yy, z = zz, color = cc) } p <- p %>% layout( updatemenus = list( ## set opacity per trace highlight single trace list(y = 0.6, buttons = lapply( levels(irs$species), function (x) { list(method = "restyle", args = list("opacity", ifelse(levels(irs$species) == x, 1, 0.1)), label = x) })), ## try set different colors points inside traces ## not working list(y = 0.4, buttons = lapply( names(irs), function(x) { list( method = "restyle", args = list( "color", split(irs[[x]], irs$species) ), label = x ) })) ) ) p
the following code solves small part of problem: how change colors of markers , lines single series.
did not find solution case multiple series.
in addition, did not find way automatic rescaling of color map after changing color variable menu. hence, rescaled "by hand" variables between 1 , 3.
realise small contribution solution of problem. anyway, hope can you.
library(plotly) library(scales) irs <- iris irs[,1:4] <- apply(irs[,1:4],2, rescale, to=c(1,3)) p <- plot_ly(data = irs, type = "scatter3d", mode = "lines+markers") p <- p %>% add_trace(x=~sepal.length, y=~sepal.width, z=~petal.length, color=~petal.width) p <- p %>% layout( updatemenus = list( list(y = 0.4, buttons = lapply( names(irs), function(x) { cols <- as.numeric(irs[,x]) list( method = "restyle", label = x, args = list( list(marker.color=list(cols), line.color=list(cols), autocolorscale=true) ) ) })) ) ) p
Comments
Post a Comment