简体   繁体   中英

R plotly fill color to next y only upper values

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?

在此处输入图像描述

    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")

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 在此处输入图像描述

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)
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM