R - One line, two colors in plotly

Viewed 37

Haven't quite found a good answer to this.

I'd like to have only one line with different colors rather than separating the two lines.

library(plotly)
library(reshape)
library(RColorBrewer)

fig <- plot_ly(big[1:100,], y = ~price, x = ~time, type = 'scatter', mode = 'lines+markers', color = ~ as.factor(dir))%>%
  layout(legend=list(title=list(text='smoker')), title = "String 'smoker' values mean discrete colors",
         plot_bgcolor='#e5ecf6')
fig

Warning messages:
1: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

2: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

enter image description here

Basically I just want to connect these lines as if they were one.

EDIT: a black line with colored points would be fine as well.


Data:

dput(head(big,100))
structure(list(time = structure(c(1647246002.24923, 1647246002.79761, 
1647246003.33618, 1647246004.39149, 1647246004.44229, 1647246005.04222, 
1647246005.61024, 1647246006.19858, 1647246006.74267, 1647246007.28133, 
1647246007.82673, 1647246007.86367, 1647246008.44411, 1647246008.98759, 
1647246009.02739, 1647246010.60053, 1647246011.15027, 1647246011.17698, 
1647246011.72366, 1647246012.79303, 1647246013.33196, 1647246013.87512, 
1647246015.96322, 1647246015.99679, 1647246016.53583, 1647246017.08083, 
1647246017.6256, 1647246018.17829, 1647246018.71606, 1647246019.25767, 
1647246019.79644, 1647246020.33468, 1647246020.8824, 1647246021.94552, 
1647246022.01591, 1647246023.6453, 1647246024.18459, 1647246024.72267, 
1647246025.26541, 1647246025.8108, 1647246026.36594, 1647246027.00413, 
1647246027.5502, 1647246028.08886, 1647246028.12655, 1647246028.19781, 
1647246029.25628, 1647246029.7948, 1647246029.82717, 1647246029.88239, 
1647246030.94222, 1647246030.98792, 1647246031.02387, 1647246031.56251, 
1647246032.10701, 1647246033.68004, 1647246034.21849, 1647246034.75698, 
1647246034.79206, 1647246035.33537, 1647246035.88073, 1647246036.41933, 
1647246037.47445, 1647246038.02743, 1647246038.56629, 1647246039.11172, 
1647246039.69961, 1647246040.24075, 1647246040.78262, 1647246040.81593, 
1647246040.84878, 1647246040.89548, 1647246041.44543, 1647246043.02019, 
1647246043.56719, 1647246044.11148, 1647246045.17907, 1647246045.73629, 
1647246046.2821, 1647246047.33841, 1647246048.92821, 1647246049.47257, 
1647246050.01503, 1647246050.04984, 1647246050.58807, 1647246051.13286, 
1647246051.6825, 1647246052.7956, 1647246053.49162, 1647246054.03274, 
1647246054.57456, 1647246055.632, 1647246055.66697, 1647246055.70239, 
1647246056.25411, 1647246056.79885, 1647246057.87931, 1647246058.42802, 
1647246060.52577, 1647246061.07338), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), price = c(13344.77, 13343.52, 13342.27, 13343.27, 
13343.02, 13343.27, 13343.27, 13342.27, 13344.77, 13345.02, 13344.52, 
13345.52, 13345.02, 13345.05, 13345.52, 13345.27, 13345.52, 13345.77, 
13346.27, 13346.52, 13345.52, 13345.02, 13345.77, 13346.02, 13345.52, 
13346.02, 13347.77, 13347.02, 13345.77, 13346.77, 13347.27, 13347.52, 
13349.02, 13349.02, 13349.27, 13350.77, 13350.52, 13351.02, 13348.77, 
13348.27, 13348.77, 13349.52, 13349.27, 13348.52, 13347.77, 13348.27, 
13348.77, 13350.77, 13350.27, 13351.27, 13351.52, 13352.02, 13351.77, 
13352.02, 13351.52, 13350.52, 13351.02, 13351.52, 13351.77, 13351.27, 
13351.52, 13351.27, 13351.27, 13351.77, 13350.77, 13351.52, 13351.77, 
13352.52, 13353.52, 13353.77, 13354.27, 13355.77, 13356.27, 13355.52, 
13354.02, 13353.77, 13353.52, 13355.27, 13355.52, 13356.27, 13357.02, 
13358.77, 13358.52, 13358.02, 13358.27, 13358.02, 13357.77, 13355.77, 
13356.02, 13356.27, 13356.02, 13356.27, 13356.52, 13356.05, 13356.52, 
13357.02, 13357.52, 13358.77, 13358.52, 13357.77), weekday = c("Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", 
"Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon", "Mon"
), threshup = c(13353.27, 13352.02, 13350.77, 13351.77, 13351.52, 
13351.77, 13351.77, 13350.77, 13353.27, 13353.52, 13353.02, 13354.02, 
13353.52, 13353.55, 13354.02, 13353.77, 13354.02, 13354.27, 13354.77, 
13355.02, 13354.02, 13353.52, 13354.27, 13354.52, 13354.02, 13354.52, 
13356.27, 13355.52, 13354.27, 13355.27, 13355.77, 13356.02, 13357.52, 
13357.52, 13357.77, 13359.27, 13359.02, 13359.52, 13357.27, 13356.77, 
13357.27, 13358.02, 13357.77, 13357.02, 13356.27, 13356.77, 13357.27, 
13359.27, 13358.77, 13359.77, 13360.02, 13360.52, 13360.27, 13360.52, 
13360.02, 13359.02, 13359.52, 13360.02, 13360.27, 13359.77, 13360.02, 
13359.77, 13359.77, 13360.27, 13359.27, 13360.02, 13360.27, 13361.02, 
13362.02, 13362.27, 13362.77, 13364.27, 13364.77, 13364.02, 13362.52, 
13362.27, 13362.02, 13363.77, 13364.02, 13364.77, 13365.52, 13367.27, 
13367.02, 13366.52, 13366.77, 13366.52, 13366.27, 13364.27, 13364.52, 
13364.77, 13364.52, 13364.77, 13365.02, 13364.55, 13365.02, 13365.52, 
13366.02, 13367.27, 13367.02, 13366.27), nextup = c(69L, 68L, 
38L, 52L, 52L, 52L, 52L, 38L, 69L, 70L, 69L, 71L, 70L, 70L, 71L, 
71L, 71L, 72L, 72L, 72L, 71L, 70L, 72L, 72L, 71L, 72L, 81L, 72L, 
72L, 72L, 73L, 73L, 82L, 82L, 82L, 103L, 103L, 103L, 82L, 81L, 
82L, 82L, 82L, 82L, 81L, 81L, 82L, 103L, 103L, 104L, 104L, 112L, 
105L, 112L, 104L, 103L, 103L, 104L, 105L, 104L, 104L, 104L, 104L, 
105L, 103L, 104L, 105L, 113L, 3724L, 3905L, 4101L, 4115L, 4116L, 
4114L, 4071L, 3905L, 3724L, 4111L, 4114L, 4116L, 4145L, 4201L, 
4201L, 4164L, 4200L, 4164L, 4164L, 4115L, 4115L, 4116L, 4115L, 
4116L, 4145L, 4115L, 4145L, 4145L, 4164L, 4201L, 4201L, 4164L
), threshdown = c(13339.77, 13338.52, 13337.27, 13338.27, 13338.02, 
13338.27, 13338.27, 13337.27, 13339.77, 13340.02, 13339.52, 13340.52, 
13340.02, 13340.05, 13340.52, 13340.27, 13340.52, 13340.77, 13341.27, 
13341.52, 13340.52, 13340.02, 13340.77, 13341.02, 13340.52, 13341.02, 
13342.77, 13342.02, 13340.77, 13341.77, 13342.27, 13342.52, 13344.02, 
13344.02, 13344.27, 13345.77, 13345.52, 13346.02, 13343.77, 13343.27, 
13343.77, 13344.52, 13344.27, 13343.52, 13342.77, 13343.27, 13343.77, 
13345.77, 13345.27, 13346.27, 13346.52, 13347.02, 13346.77, 13347.02, 
13346.52, 13345.52, 13346.02, 13346.52, 13346.77, 13346.27, 13346.52, 
13346.27, 13346.27, 13346.77, 13345.77, 13346.52, 13346.77, 13347.52, 
13348.52, 13348.77, 13349.27, 13350.77, 13351.27, 13350.52, 13349.02, 
13348.77, 13348.52, 13350.27, 13350.52, 13351.27, 13352.02, 13353.77, 
13353.52, 13353.02, 13353.27, 13353.02, 13352.77, 13350.77, 13351.02, 
13351.27, 13351.02, 13351.27, 13351.52, 13351.05, 13351.52, 13352.02, 
13352.52, 13353.77, 13353.52, 13352.77), nextdown = c(540L, 541L, 
547L, 541L, 541L, 541L, 541L, 547L, 540L, 233L, 540L, 233L, 233L, 
233L, 233L, 233L, 233L, 233L, 233L, 233L, 233L, 233L, 233L, 233L, 
233L, 233L, 220L, 230L, 233L, 233L, 230L, 226L, 196L, 196L, 196L, 
195L, 195L, 195L, 196L, 218L, 196L, 196L, 196L, 217L, 220L, 218L, 
196L, 195L, 195L, 195L, 195L, 195L, 195L, 195L, 195L, 195L, 195L, 
195L, 195L, 195L, 195L, 195L, 195L, 195L, 195L, 195L, 195L, 194L, 
190L, 190L, 189L, 184L, 184L, 185L, 189L, 190L, 190L, 185L, 185L, 
184L, 184L, 182L, 182L, 183L, 183L, 183L, 183L, 184L, 184L, 184L, 
184L, 184L, 184L, 184L, 184L, 184L, 183L, 182L, 182L, 183L), 
    dir = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, -1L, -1L, -1L, 
    -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 
    -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 
    -1L, -1L, -1L, -1L), color = c("green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "green", "green", "green", "green", "green", 
    "green", "green", "red", "red", "red", "red", "red", "red", 
    "red", "red", "red", "red", "red", "red", "red", "red", "red", 
    "red", "red", "red", "red", "red", "red", "red", "red", "red", 
    "red", "red", "red", "red", "red", "red", "red", "red")), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"), .internal.selfref = <pointer: 0x000001bc75c81ef0>)
1 Answers

An option could be that you duplicated the first row per group and that you make the first row of the latter group (dir = -1) make dir = 1, so that the first line connect to the start of the second line like this:

library(plotly)
library(reshape)
library(RColorBrewer)
library(dplyr)

big <- big %>%
  group_by(color) %>%
  slice(1L, row_number()) %>%
  mutate(dir = ifelse(row_number() == 1 & dir == -1, 1, dir)) %>%
  ungroup()
  
fig <- plot_ly(big[1:100,], y = ~price, x = ~time, type = 'scatter', mode = 'lines+markers', color = ~ as.factor(dir))%>%
  layout(legend=list(title=list(text='smoker')), title = "String 'smoker' values mean discrete colors",
         plot_bgcolor='#e5ecf6')
fig
#> Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
#> Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Created on 2022-08-17 by the reprex package (v2.0.1)

Related