提问人:Andrew 提问时间:10/10/2022 更新时间:10/10/2022 访问量:476
在 plotly R 上绘制矩形
drawing rectangle on plotly R
问:
我想在使用日期的绘图上绘制一个矩形多边形。作为一个玩具的例子,我的代码是
library(tidyverse)
library(plotly)
df <- tibble(daydate = dmy(c("01-01-2022", "01-02-2022", "15-02-2022", "27-02-2022")),
value = runif(4, 0, 100))
plot_ly(df, x = ~daydate, y = ~value, color = I("#9B4393"), type = 'scatter', mode = 'lines')
我想绘制一个跨越日期“01-02-2022”到“15-02-2022”以及从 0 到 100 的矩形。另外,我希望矩形区域是灰色的,这是相当透明的。
非常感谢
答:
1赞
SamR
10/10/2022
#1
定义一个矩形:
my_rectangle <- list(
type = "rect",
fillcolor = "grey",
line = list(color = "black"),
opacity = 0.3,
x0 = "2022-02-01",
x1 = "2022-02-15",
xref = "x",
y0 = 0,
y1 = 100,
yref = "y"
)
将其添加到绘图中:
plot_ly(
df,
x = ~daydate,
y = ~value,
color = I("#9B4393"),
type = "scatter",
mode = "lines"
) |>
layout(
shapes = list(
my_rectangle
)
)
R 绘图文档阐述了如何绘制矩形和各种其他形状并设置其样式。
评论