Reputation: 9809
I need to plot 2 traces, one on the left y-axis and one on the right and I would like to keep the highlight function. So the data
argument is not the same for the two traces.
Is there a way to do that?
library(plotly)
## Data ################
tmp <- data.frame(timestamp = structure(c(1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200,
1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200),
class = c("POSIXct", "POSIXt"), tzone = "Europe/Berlin"),
value = c(29.015, 25.724, 24.017, 25.118, 28.334, 30.408,
32.046, 32.441, 29.463, 27.528, 24.136, 21.067, 27.352, 23.13,
24.962, 24.558, 130.475, 148.465, 157.134, 149.983, 133.003,
123.9, 117.334, 115.927),
valuetype = c(rep("Type 1",12),rep("Type 2",12)),
route_name = rep("group 1", 24),
value_color = c(rep("#e3a201", 12), rep("#FFBA00",12)))
## Plotly ###############
plot_ly(data = tmp) %>%
highlight_key(~paste0(route_name, valuetype)) %>%
## 1 Trace ###################
add_trace(
data = tmp[tmp$valuetype %in% unique(tmp$valuetype)[[1]],],
inherit = FALSE,
x = ~timestamp, y = ~value,
type = "scattergl", mode = "lines",
name = ~paste(valuetype, route_name),
color = ~I(value_color)) %>%
## 2 Trace ###################
add_trace(
data = tmp[tmp$valuetype %in% unique(tmp$valuetype)[[2]],],
inherit = FALSE,
x = ~timestamp, y = ~value,
yaxis = "y2",
type = "scattergl", mode = "lines",
name = ~paste(valuetype, route_name),
color = ~I(value_color)) %>%
## Layout for Group 1&2 ###################
plotly::layout(xaxis = list(title = "Zeit", type = "date"),
yaxis = list(side ="left", title = "Y-Title"),
yaxis2 = list(side="right", overlaying = "y", title = "Y2-Title"),
margin = list(l=50,r=50,t=50,b=10),
legend = list(orientation = 'h', xanchor = 'center', x = 0.5, y = -0.2)) %>%
plotly::highlight(on = "plotly_hover", off = "plotly_doubleclick",
selected = attrs_selected(showlegend = FALSE))
Upvotes: 1
Views: 261
Reputation: 33417
Not sure if this is an option for you, but if you use only a single data
argument you can use style
to assign a trace to another y-axis:
library(plotly)
## Data ################
tmp <- data.frame(timestamp = structure(c(1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200,
1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200),
class = c("POSIXct", "POSIXt"), tzone = "Europe/Berlin"),
value = c(29.015, 25.724, 24.017, 25.118, 28.334, 30.408,
32.046, 32.441, 29.463, 27.528, 24.136, 21.067, 27.352, 23.13,
24.962, 24.558, 130.475, 148.465, 157.134, 149.983, 133.003,
123.9, 117.334, 115.927),
valuetype = c(rep("Type 1",12),rep("Type 2",12)),
route_name = rep("group 1", 24),
value_color = c(rep("#e3a201", 12), rep("#FFBA00",12)))
## Plotly ###############
tmp %>% highlight_key(~paste0(route_name, valuetype)) %>%
plot_ly(
x = ~timestamp, y = ~value,
type = "scatter", mode = "lines", # "scattergl"
name = ~paste(valuetype, route_name),
color = ~I(value_color)) %>%
## assign trace 1 to y2
style(yaxis = "y2", traces = 1) %>%
## Layout for Group 1&2 ###################
plotly::layout(xaxis = list(title = "Zeit", type = "date"),
yaxis = list(side ="left", title = "Y-Title"),
yaxis2 = list(side="right", overlaying = "y", title = "Y2-Title"),
margin = list(l=50,r=50,t=50,b=10),
legend = list(orientation = 'h', xanchor = 'center', x = 0.5, y = -0.2)) %>%
plotly::highlight(on = "plotly_hover", off = "plotly_doubleclick", selected = attrs_selected(showlegend = FALSE))
Edit: Trace indices example:
library(plotly)
library(data.table)
## Data ################
tmp <- data.frame(timestamp = structure(c(1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200,
1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200,
1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200,
1632132000, 1632139200, 1632146400, 1632153600, 1632160800, 1632168000,
1632175200, 1632182400, 1632189600, 1632196800, 1632204000, 1632211200),
class = c("POSIXct", "POSIXt"), tzone = "Europe/Berlin"),
value = c(c(29.015, 25.724, 24.017, 25.118, 28.334, 30.408,
32.046, 32.441, 29.463, 27.528, 24.136, 21.067, 27.352, 23.13,
24.962, 24.558, 130.475, 148.465, 157.134, 149.983, 133.003,
123.9, 117.334, 115.927), c(29.015, 25.724, 24.017, 25.118, 28.334, 30.408,
32.046, 3.441, 2.463, 2.528, 2.136, 2.067, 2.352, 23.13,
24.962, 24.558, 13.475, 14.465, 15.134, 14.983, 13.003,
123.9, 117.334, 115.927) + 50),
valuetype = c(rep("Type 1",12), rep("Type 2", 12), rep("Type 3", 12), rep("Type 4", 12)),
route_name = rep("group 1", 48),
value_color = c(rep("#e3a201", 12), rep("#FFBA00", 12), rep("red", 12), rep("darkgreen", 12)),
yaxis = c(rep("y", 12), rep("y2", 12), rep("y", 12), rep("y3", 12)))
setDT(tmp)
tmp[, trace_index := .GRP-1, by = .(route_name, valuetype)]
## Plotly ###############
shared_data <- highlight_key(tmp, ~paste0(route_name, valuetype))
fig <- plot_ly(data = shared_data,
x = ~timestamp, y = ~value,
type = "scatter", mode = "lines", # "scattergl"
name = ~paste(valuetype, route_name),
color = ~I(value_color))
if(any(tmp$yaxis != "y")){
axesMapping <- unique(tmp[yaxis != "y", c("yaxis","trace_index")])
# layout call to add the axis could be added here
for(i in seq_len(NROW(axesMapping))){
fig <- do.call(plotly::style, list(p = fig, yaxis = axesMapping[i,]$yaxis, traces = axesMapping[i,]$trace_index))
}
}
## Layout for Group 1&2 ###################
plotly::layout(fig,
xaxis = list(title = "Zeit", type = "date", domain = c(0, 0.75)),
yaxis = list(side ="left", title = "Y-Title"),
yaxis2 = list(side="right", overlaying = "y", title = "Y2-Title"),
yaxis3 = list(side="right", overlaying = "y", anchor="free", position = 0.85, title = "Y3-Title"),
margin = list(l=50,r=50,t=50,b=10),
legend = list(orientation = 'h', xanchor = 'center', x = 0.5, y = -0.2)) %>%
plotly::highlight(on = "plotly_hover", off = "plotly_doubleclick", selected = attrs_selected(showlegend = FALSE))
Upvotes: 1