I’m looking to make a plot like the below.
I run into problems with keeping the color of the line separate of the markers. I can make them separate if I first draw the markers, then the line:
library(plotly)
#generating data
set.seed(1)
x <- seq(1,13,1)
y <- rbinom(13,1,0.4)
signal <- ifelse(y == 1, 'sig','noSig')
df <- data.frame(x = x,y=y,signal=signal)
#plotting markers first, then line
fig <- plot_ly()
fig <- fig %>% add_trace(data = df,x=~x,y=~y,
mode='markers',type='scatter',color=~signal,colors=c("black","red"))
fig <- fig %>% add_trace(x = df$x,y=df$y,
mode = 'lines',type='scatter',line=list(color="black"),showlegend=F)
fig
However, I want the markers on top of the line, so why don’t I get the desired output if I draw the line first?
fig <- plot_ly()
fig <- fig %>% add_trace(x = df$x,y=df$y,
mode = 'lines',type='scatter',line=list(color="black"),showlegend=F)
fig <- fig %>% add_trace(data = df,x=~x,y=~y,
mode='markers',type='scatter',color=~signal,colors=c("black","red"))
fig
I also receive the following warning:
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
Any suggestions on how to draw a plot like the first plot, with the black line behind the colored points?