使用形状以图形方式添加背景部分颜色

时间:2018-06-08 23:59:23

标签: 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, xmax= Inf, ymin= 0.2, ymax= 0.8),alpha=0.3, fill="#514e4f") 

enter image description here

不幸的是,当通过ggplotly将ggplot对象转换为绘图对象时,背景形状不会被保留:

p.plotly <- ggplotly(p.ggplot)

enter image description here

按照https://plot.ly/r/shapes/上的说明,我将以下行添加到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")

0 个答案:

没有答案