R 使用图形在plotly中添加背景剖面颜色

R 使用图形在plotly中添加背景剖面颜色,r,ggplot2,plotly,r-plotly,ggplotly,R,Ggplot2,Plotly,R Plotly,Ggplotly,我使用ggplot创建了一个绘图,在绘图的某些区域添加了背景色: p.ggplot + geom_rect(aes(xmin= -Inf, xmax= Inf, ymin= -5, ymax= 0.2),alpha=0.3, fill="#b71e3f") + geom_rect(aes(xmin= -Inf, xmax= Inf, ymin= 0.8, ymax= 1),alpha=0.3, fill="#1f59b7") + geom_rect(aes(xmin= -Inf, x

我使用ggplot创建了一个绘图,在绘图的某些区域添加了背景色:

p.ggplot +
  geom_rect(aes(xmin= -Inf, xmax= Inf, ymin= -5, ymax= 0.2),alpha=0.3, fill="#b71e3f") +
  geom_rect(aes(xmin= -Inf, xmax= Inf, ymin= 0.8, ymax= 1),alpha=0.3, fill="#1f59b7") +
  geom_rect(aes(xmin= -Inf, xmax= Inf, ymin= 0.2, ymax= 0.8),alpha=0.3, fill="#514e4f") 

遗憾的是,通过ggplotly将ggplot对象转换为plotly对象时,背景形状不受保护:

p.plotly <- ggplotly(p.ggplot)

p.plotly如果您提供一个可复制的示例,我们将更容易帮助您,这样我们就不必重建一些样本数据,也不必猜测您用于创建绘图的代码。
p.plotly <- layout(p.plotly,

            shapes = list(
                         list(type = "rect",
                              fillcolor = "#b71e3f", line = list(color = "#b71e3f"), opacity = 0.3,
                              x0 = 0, x1 = 2, xref = "x",
                              y0 = -5, y1 = 0.2, yref = "y"),

                         list(type = "rect",
                               fillcolor = "#1f59b7", line = list(color = "#1f59b7"), opacity = 0.3,
                               x0 = 0, x1 = 2, xref = "x",
                               y0 = 0.2, y1 = 0.8, yref = "y"),

                           list(type = "rect",
                               fillcolor = "#514e4f", line = list(color = "#514e4f"), opacity = 0.3,
                               x0 = 0, x1 = 2, xref = "x",
                               y0 = 0.8, y1 = 1, yref = "y")))

htmlwidgets::saveWidget(as_widget(p.plotly), "sexCheckPlot.html")