Sup'A
Sup'A

Reputation: 119

Plotly: How to set background color per data

I want to set two different colors as the plot background: one color per data like this

reference chart

Here are my plots so far:

My Chart

Upvotes: 1

Views: 6751

Answers (1)

vestland
vestland

Reputation: 61214

The following suggestion is a minimal reproducible example to show you that it can be done. Albeit not so easily and elegantly as you may have hoped. To my knowledge, there does not yet exist a way to alternate backround colors without doing so through shapes. The following setup at least captures some of the features of your demonstrated setup, in that it is a mix of a line and a bar chart, and that it produces multiple subplots in one go.

Plot:

enter image description here

Code:

library(dplyr)
library(plotly)

df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),
                y1=c(4,2,4,2,6,7,8,7,9,10,9,12),
                y2=c(3,4,5,6,3,1,5,6,3,2,7,8))


list(df$x)

x_start <- df$x[seq(1, length(df$x), 2)]
x_stops <- df$x[seq(2, length(df$x), 2)]

p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')
p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)

# set up shapes
shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape_offset = 0.5
shapes <- list()

for (i in seq_along(x_start)){
  print(i)
  shape[["x0"]] <- x_start[i] + shape_offset
  shape[["x1"]] <- x_stops[i] + shape_offset
  shape[["y0"]] <- 0
  shape[["y1"]] <- 16
  shapes <- c(shapes, list(shape))
}

p1 <- layout(p1, shapes=shapes, xaxis = list(showgrid=FALSE))                 
p2 <- layout(p2, shapes=shapes)                 


p <- subplot(p1, p2, nrows = 2, margin=0.05)

p

I hope this will be useful to you. If you'd like we can discuss further details when you've had a chance to look at it.

Edit 1:

Here's a suggestion that takes the max of y into consideration when the background shapes are built. This can be made flexible with regards to the number of subplots if you're interested.

Plot 2:

enter image description here

Code 2:

library(dplyr)
library(plotly)

df = data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),
                y1=c(4,2,4,2,6,7,8,7,9,10,9,12),
                y2=c(3,4,5,6,3,1,5,6,3,2,7,8))


list(df$x)

x_start <- df$x[seq(1, length(df$x), 2)]
x_stops <- df$x[seq(2, length(df$x), 2)]

p1 <- plot_ly(x=df$x, y=df$y1, mode='lines', line=list(color='green'), name = 'line')
p2 <- plot_ly(df) %>% add_bars(x=~x, y=~y2, name='bar', width=0.4)

# set up plot 1 shapes
shape=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape_offset = 0.5

shapes <- list()


for (i in seq_along(x_start)){
  print(i)
  shape[["x0"]] <- x_start[i] + shape_offset
  shape[["x1"]] <- x_stops[i] + shape_offset
  shape[["y0"]] <- 0
  shape[["y1"]] <- max(df$y1)
  shapes <- c(shapes, list(shape))
  #print()
}

# set up plot 2 shapes
shape2=list(type='rect', line = list(color = 'rgba(0,0,0,0)'), fillcolor="rgba(147,112,219,0.1)", xref='x', yref='y')
shape2_offset = 0.5

shapes2 <- list()


for (i in seq_along(x_start)){
  print(i)
  shape2[["x0"]] <- x_start[i] + shape2_offset
  shape2[["x1"]] <- x_stops[i] + shape2_offset
  shape2[["y0"]] <- 0
  shape2[["y1"]] <- max(df$y2)
  shapes2 <- c(shapes2, list(shape2))
  #print()
}

p1 <- layout(p1, shapes=shapes, xaxis = list(showgrid=FALSE))                 
p2 <- layout(p2, shapes=shapes2)                 


p <- subplot(p1, p2, nrows = 2, margin=0.05)

p

Upvotes: 3

Related Questions