1

I have a magneta colored graph made in plotly with certain levels which I have added via vertical lines. Currently I have color filled these areas between the vertical lines.

However I would like to only have a color between the vertical line and the magneta graph (I have circled the aereas as a reference.

Is this possible in plotly?

enter image description here

    plot <- plot %>% add_lines(type = 'scatter', mode = "lines", 
                                   name = "test", yaxis = 'y2',
                                   x = ~t, y = ~v.x,
                                   line = list(color = '#CC79A7'),
                                   hoverinfo = "text",
                                   text = ~paste(round(v.x, 1), t))
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t), 
                                      y = round(quantNUPL[1], 1), yend = round(quantNUPL[1], 1), yaxis = 'y2',
                                      line= list(color = "#0072B0",
                                                 dash = "dash",
                                                 width = 1),
                                      showlegend = F)
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t), 
                                      y = round(quantNUPL[2], 1), yend = round(quantNUPL[2], 1), yaxis = 'y2',
                                      line= list(color = "#0072B0",
                                                 dash = "dash",
                                                 width = 1),
                                      fill = 'tonexty',
                                      fillcolor = '#56B4E940',
                                      name = "90% UPSIDE")
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t), 
                                      y = round(quantNUPL[6], 1), yend = round(quantNUPL[6], 1), yaxis = 'y2',
                                      line= list(color = "#999999",
                                                 dash = "dash",
                                                 width = 1),
                                      name = "50% UPSIDE")
                                                        
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t), 
                                      y = round(quantNUPL[9], 1), yend = round(quantNUPL[9], 1), yaxis = 'y2',
                                      line= list(color = "#E69F00",
                                                 dash = "dot",
                                                 width = 1),
                                      name = "20% UPSIDE",
                                      showlegend = F)
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t), 
                                      y = round(quantNUPL[10], 1), yend = round(quantNUPL[10], 1), yaxis = 'y2',
                                      line= list(color = "#E69F00",
                                                 dash = "dash",
                                                 width = 1),
                                      fill = 'tonexty',
                                      fillcolor = "#E69F0020",
                                      name = "20% UPSIDE")
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t),
                                      y = round(quantNUPL[11], 1), yend = round(quantNUPL[11], 1), yaxis = 'y2',
                                      line= list(color = "#E69F00",
                                                 dash = "solid",
                                                 width = 1),
                                      fill = 'tonexty',
                                      fillcolor = "#E69F0040",
                                      name = "10% UPSIDE")
H. berg
  • 471
  • 1
  • 3
  • 11
  • FYI somehow plotly filles in every part below and above a vertical line with the area of the magneta line which I obviously dont want.....here is btw the documtation: https://plotly.com/r/reference/#scatter-fill – H. berg Mar 07 '21 at 18:04
  • 1
    Please keep in mind that it is more likely to get your problem solved if you create a small reproducible example. That means you need to add data or an example dataset. In your code values for `t` and `quantNUPL` are missing. Please have a look at these information: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – tamtam Mar 08 '21 at 18:18

1 Answers1

2

You can try setting a trace and defining the max y-values with an ifelse-function.

Example Data

df <- data.frame(x = 1:50,
                 y = c(20:5,6:10,9:3,4:20, 19:15))

Code

plot_ly(df) %>% 
  add_lines(type = 'scatter', mode = "lines", 
            name = "test", yaxis = 'y2',
            x = ~x, y = ~y,
            line = list(color = '#CC79A7'),
            hoverinfo = "text",
            text = ~paste(round(y, 1), x)) %>%
  #color space between y = 8 and line
  add_trace(x = ~x, y = ~ifelse(y <=8, y, 8),
            type = "scatter", fill = "toself", 
            fillcolor = "blue", mode = "none",
            yaxis = 'y2') %>%
  #background boxes
  layout(shapes = list(
    list(type = "rect",
         fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
         x0 = ~min(x), x1 = ~max(x),
         y0 = 0, y1 = 8, yref = "y2"),
    list(type = "rect",
         fillcolor = "orange", line = list(color = "orange"), opacity = 0.3,
         x0 = ~min(x), x1 = ~max(x),
         y0 = 18, y1 = 20, yref = "y2"),
    list(type = "rect",
         fillcolor = "yellow", line = list(color = "yellow"), opacity = 0.3,
         x0 = ~min(x), x1 = ~max(x),
         y0 = 16, y1 = 20, yref = "y2")))

Plot enter image description here

Second idea

df <- data.frame(x = 1:50,   
                 y = c(20:5,6:10,9:3,4:8,1,10:20, 5:1)) 

plot_ly(df) %>% 
  # add darkblue background
  add_trace(x = ~x, y = 8,
            type = "scatter", fill = "tozeroy",
            fillcolor = "blue", mode = "none",
            yaxis = 'y2', hoverinfo = "none") %>%
  # set area underneath red line to white
  add_lines(type = 'scatter', mode = "lines",
           yaxis = 'y2',
            x = ~x, y = ~y,
            line = list(width = 0),
            hoverinfo = "none",
            fillcolor = "white",
            fill = "tozeroy",
            showlegend = FALSE) %>%
  # add red line graph
  add_lines(type = 'scatter', mode = "lines", 
            name = "test", yaxis = 'y2',
            x = ~x, y = ~y,
            line = list(color = '#CC79A7'),
            hoverinfo = "text",
            text = ~paste(round(y, 1), x)) %>%
  layout(shapes = list(
    # add lightblue background
    list(type = "rect",
         fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
        x0 = ~min(x), x1 = ~max(x),
        y0 = 0, y1 = 8, yref = "y2"),
    # add orange background
    list(type = "rect",
         fillcolor = "orange", line = list(color = "orange"), opacity = 0.3,
         x0 = ~min(x), x1 = ~max(x),
         y0 = 18, y1 = 20, yref = "y2"),
    #add yellow backround
    list(type = "rect",
         fillcolor = "yellow", line = list(color = "yellow"), opacity = 0.3,
         x0 = ~min(x), x1 = ~max(x),
         y0 = 16, y1 = 20, yref = "y2")),
    #remove grid
    xaxis = list(showgrid = F),
    yaxis2 = list(showgrid = F)
    )
tamtam
  • 3,541
  • 1
  • 7
  • 21