0

I'm using Plotly in R and I want to create a plot like this: Fill to y0

But when trying with fill = "tozeroy" the plot looks like this: fill tozeroy

Ignoring the value of y0

So, I want to fill the area from the lines to y0 value, instead to absolute zero.

A code example:

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 = ""))

produces: CodeExample output

Ignoring the y0 = 80 parameter.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Alnair
  • 938
  • 8
  • 10
  • 1
    please provide a reproducible example. This will make it much easier for others to help you – vsb Feb 26 '18 at 09:39

1 Answers1

2

If I understood correctly you want the range to begin in y0. To that end, you have to change the yaxis' range in the layout.

This should suffice:

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 = ""))

The range options in plotly are explained here

Jon Nagra
  • 1,538
  • 1
  • 16
  • 36