我有一个磁铁彩色图表,它是用某些水平绘制的,我通过垂直线添加了这些水平。目前我已经用颜色填充了垂直线之间的这些区域。
但是,我只想在垂直线和磁力图之间有一种颜色(我已经圈出了区域作为引用。
这在 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")
最佳答案
您可以尝试设置跟踪并使用 ifelse 函数
定义最大 y 值。
示例数据
df <- data.frame(x = 1:50,
y = c(20:5,6:10,9:3,4:20, 19:15))
代码
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")))
第二个想法
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)
)
关于R仅将颜色填充到下一个y的上值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66519669/