0

我在 R 中使用 Plotly,我想创建一个这样的图: 填充到 y0

但是当尝试使用 fill = "tozeroy" 时,情节看起来像这样: 填充清零

忽略 y0 的值

所以,我想将区域从线条填充到 y0 值,而不是绝对零。

代码示例:

library(plotly)
library(magrittr)

x <- seq.Date(as.Date("2017/01/01"), as.Date("2017/12/31"), by = 1)
y1 <- rnorm(365, 100, 10)
y2 <- rnorm(365, 100, 10)

dat <- data.frame(x, y1, y2)

plot_ly(dat, x = ~x, y = ~y1, mode = "lines", type = "scatter", fill = "tozeroy", y0 = 80, name = "y1") %>%
add_trace(dat, x = ~x, y = ~y2, fill = 'tozeroy', y0 = 80, name = "y2")%>%
layout(xaxis = list(title = ""), yaxis = list(title = ""))

产生: 代码示例输出

忽略y0 = 80参数。

4

1 回答 1

2

如果我理解正确,您希望范围以y0. 为此,您必须更改yaxis' 中的范围layout

这应该足够了:

library(plotly)
library(magrittr)
set.seed(19)

x <- seq.Date(as.Date("2017/01/01"), as.Date("2017/12/31"), by = 1)
y1 <- rnorm(365, 100, 10)
y2 <- rnorm(365, 100, 10)

y0 <- 80
ymax <- max(y1,y2)

dat <- data.frame(x, y1, y2)

plot_ly(dat, x = ~x, y = ~y1, mode = "lines", type = "scatter", fill = "tozeroy", y0 = 80, name = "y1") %>%
  add_trace(dat, x = ~x, y = ~y2, fill = 'tozeroy',  name = "y2")%>%
  layout(xaxis = list(title = ""), yaxis = list(range = c(y0, ymax),title = ""))

这里plotly解释了范围选项

于 2018-02-26T11:29:41.087 回答