如何将绘图填充到给定的 y0 值
How fill a plot to a given y0 value
我在 R 中使用 Plotly,我想创建一个这样的图:
但是当尝试使用 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
参数。
如果我理解正确的话,您希望范围从 y0
开始。
为此,您必须更改 layout
.
中的 yaxis
' 范围
这应该足够了:
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
中的范围选项解释here
我在 R 中使用 Plotly,我想创建一个这样的图:
但是当尝试使用 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
参数。
如果我理解正确的话,您希望范围从 y0
开始。
为此,您必须更改 layout
.
yaxis
' 范围
这应该足够了:
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
中的范围选项解释here