您可以使用layout
中的shapes
参数来生成这样的plotly
图:
library(plotly)
library(tibble)
library(purrr)
lims <- local({
lims <- c(0L, 48L, 69L, 100L)
tibble(left = head(lims, -1L),
right = tail(lims, -1L),
color = c("firebrick", "darkorange", "forestgreen"))
})
shps <- pmap(lims, function(left, right, color) {
list(
type = "rect",
fillcolor = color,
line = list(width = 0),
x0 = left,
x1 = right,
y0 = 0,
y1 = 1,
layer = "below"
)
})
shps <- c(shps,
list(
list(
type = "rect",
fillcolor = "#000",
line = list(color = "#fff", width = 2),
x0 = 44,
x1 = 73,
y0 = .35,
y1 = .65,
layer = "below"
)
)
)
plot_ly(width = 1000, height = 200) %>%
add_text(x = 60, y = .5, text = "Add a Label", textfont = list(color = "#fff")) %>%
layout(shapes = shps,
xaxis = list(ticks = "outside", zeroline = FALSE, showgrid = FALSE),
yaxis = list(showticklabels = FALSE, zeroline = TRUE, showgrid = FALSE,
zerolinecolor = "#000"))