Hi plotly community
I am currently creating an app and use plotly ®.
I am already happy with the graph itself, but something that seems basic to me turned out to be a roadblock for me…
I am trying to change the color of a trace of n-traces in a scatter plot using a drop down selection button. All lines are grey and the one selected should be highlighted given a defined color i.e. red.
I was able to change the color of all lines, but could not find a solution yet to change just one line.
I highlighted the parts which should change the coloring with comments “# <--------”.
Here is the current code with an example dataset:
library(plotly)
library(dplyr)
library(lubridate)
library(plyr)
df = data.frame(Team = c(rep(c("Team1", "Team2"),5)), Zwischenpunkt = rep(seq(1:5),each=2), value = c(0,0, 253,258,403,469,562,647,756,911))
fig <- df
# helper function for animation
# can be ignored
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
# adding frame to the dataset for animation
# can be ignored
fig <- fig %>% accumulate_by(~Zwischenpunkt)
# used to label axis manually
# can be ignored
max_value <- plyr::round_any(max(fig$value), 100, ceiling)
steps <- seq(0, max_value, max_value / 10)
steps_in_min <- seconds_to_period(steps)
# currently used for coloring
# might be necessary to solve question
n_teams <- length(levels(fig$Team)) # <--------
# this should assign the color red to the selected team
# currently not working.
# this is used within %>% layout(...)
# -------->
interactive_color_by_team = list(
list(
buttons = lapply(levels(fig$Team),
function(x) {
cols <- rep("grey", n_teams)
ind_new_col <- min(which(fig$Team == x))
cols[ind_new_col] <- "red"
list(method = "restyle",
label = x,
args = list(list(colors = list(cols))))
}
)
)
)
# <--------
# simple plot -> created multiple lines using 'split'
fig1 <- fig %>%
plot_ly(
x = ~Zwischenpunkt,
y = ~value,
split = ~Team,
frame = ~frame,
type = 'scatter',
mode = 'lines+markers',
color = ~Team, # <--------
colors = c('grey', "grey", "grey", "grey","grey") # <--------
# line = list(simplyfy = F,
# color = c(rep('rgba(204, 204, 204,1)', n_teams)), width = c(rep(2,n_teams)))
# color = c(rep('grey', n_teams)), width = c(rep(2,n_teams)))
# colors = c('grey', "red", "grey", "grey","grey"), width = c(rep(2,n_teams)))
)
# this is where the updatemenue is which we likely need to change the color!
# -- updatemenues --
fig1 <- fig1 %>% layout(
updatemenus = interactive_color_by_team, # <--------
xaxis = list(
title = " ",
zeroline = FALSE,
ticktext = list("Start", "Rätsel 1", "Rätsel 2", "Rätsel 3", "Totale Zeit"),
tickvals = list(1, 2, 3, 4, 5),
# tickmode = "array",
showline = TRUE,
showgrid = FALSE,
# showticklabels = TRUE,
linecolor = 'rgb(204, 204, 204)',
linewidth = 2,
# autotick = FALSE,
# ticks = 'outside',
tickcolor = 'rgb(204, 204, 204)',
tickwidth = 2,
ticklen = 5,
tickfont = list(family = 'Arial',
size = 12,
color = 'rgb(82, 82, 82)')
),
yaxis = list(
title = " ",
ticktext = as.list(as.character(steps_in_min)),
tickvals = as.list(steps),
tickmode = "array",
showgrid = FALSE,
zeroline = FALSE,
showline = TRUE,
linecolor = 'rgb(204, 204, 204)',
linewidth = 2,
tickcolor = 'rgb(204, 204, 204)',
tickwidth = 2,
ticklen = 5,
tickfont = list(family = 'Arial',
size = 12,
color = 'rgb(82, 82, 82)'),
showticklabels = TRUE
),
title = "Animated Gametime",
autosize = TRUE,
showlegend = FALSE
)
# can be ignored - just for animation
fig1 <- fig1 %>% animation_opts(
frame = 2000,
transition = 0,
redraw = FALSE
)
# can be ignored - just for animation
fig1 <- fig1 %>% animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
fig1