Следующее предложение является минимальным воспроизводимым примером, чтобы показать вам, что можно сделать. Хотя не так легко и элегантно, как вы могли надеяться. Насколько мне известно, пока не существует способа чередовать фоновые цвета без этого через формы . Следующая установка, по крайней мере, отражает некоторые особенности вашей демонстрационной установки, в том, что она представляет собой сочетание линии и гистограммы, и что она создает несколько вспомогательных участков в одном go.
. Сюжет:
![enter image description here](https://i.stack.imgur.com/TpZ4T.png)
Код:
library(dplyr)
library(plotly)
df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),
y1=c(4,2,4,2,6,7,8,7,9,10,9,12),
y2=c(3,4,5,6,3,1,5,6,3,2,7,8))
list(df$x)
x_start <- df$x[seq(1, length(df$x), 2)]
x_stops <- df$x[seq(2, length(df$x), 2)]
p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')
p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)
# set up shapes
shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape_offset = 0.5
shapes <- list()
for (i in seq_along(x_start)){
print(i)
shape[["x0"]] <- x_start[i] + shape_offset
shape[["x1"]] <- x_stops[i] + shape_offset
shape[["y0"]] <- 0
shape[["y1"]] <- 16
shapes <- c(shapes, list(shape))
}
p1 <- layout(p1, shapes=shapes, xaxis = list(showgrid=FALSE))
p2 <- layout(p2, shapes=shapes)
p <- subplot(p1, p2, nrows = 2, margin=0.05)
p
Надеюсь, это будет полезно вы. Если вы хотите, мы можем обсудить дальнейшие подробности, когда у вас будет возможность взглянуть на это.
Редактировать 1:
Вот предложение, которое берет максимум у во внимание, когда фоновые фигуры строятся. Это может быть сделано гибким в отношении количества участков, если вы заинтересованы.
Участок 2:
![enter image description here](https://i.stack.imgur.com/5YGME.png)
Код 2:
library(dplyr)
library(plotly)
df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),
y1=c(4,2,4,2,6,7,8,7,9,10,9,12),
y2=c(3,4,5,6,3,1,5,6,3,2,7,8))
list(df$x)
x_start <- df$x[seq(1, length(df$x), 2)]
x_stops <- df$x[seq(2, length(df$x), 2)]
p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')
p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)
# set up plot 1 shapes
shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape_offset = 0.5
shapes <- list()
for (i in seq_along(x_start)){
print(i)
shape[["x0"]] <- x_start[i] + shape_offset
shape[["x1"]] <- x_stops[i] + shape_offset
shape[["y0"]] <- 0
shape[["y1"]] <- max(df$y1)
shapes <- c(shapes, list(shape))
#print()
}
# set up plot 2 shapes
shape2=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape2_offset = 0.5
shapes2 <- list()
for (i in seq_along(x_start)){
print(i)
shape2[["x0"]] <- x_start[i] + shape2_offset
shape2[["x1"]] <- x_stops[i] + shape2_offset
shape2[["y0"]] <- 0
shape2[["y1"]] <- max(df$y2)
shapes2 <- c(shapes2, list(shape2))
#print()
}
p1 <- layout(p1, shapes=shapes, xaxis = list(showgrid=FALSE))
p2 <- layout(p2, shapes=shapes2)
p <- subplot(p1, p2, nrows = 2, margin=0.05)
p