0

I am attempting to highlight individual charts inside of a group of charts. In the example below, I want to highlight the 'cash_lvl' plot. If you run the code below you can see that dat.plot.list[1] will display a chart with a red background. However, when subplot is used, both of the backgrounds are white.

Is there any way to get the background to show correctly in the subplot?

library(dplyr)
library(plotly)

dat <- structure(
    list(
      date = structure(
        c(
          16101, 16129, 16160, 16190,
          16221, 16251, 16282, 16313, 16343, 16374, 16404, 16435, 16101,
          16129, 16160, 16190, 16221, 16251, 16282, 16313, 16343, 16374,
          16404, 16435
        ), class = "Date"
      ), measure = c(
        "cash_lvl", "cash_lvl",
        "cash_lvl", "cash_lvl", "cash_lvl", "cash_lvl", "cash_lvl", "cash_lvl",
        "cash_lvl", "cash_lvl", "cash_lvl", "cash_lvl", "tot_eq_lvl",
        "tot_eq_lvl", "tot_eq_lvl", "tot_eq_lvl", "tot_eq_lvl", "tot_eq_lvl",
        "tot_eq_lvl", "tot_eq_lvl", "tot_eq_lvl", "tot_eq_lvl", "tot_eq_lvl",
        "tot_eq_lvl"
      ), value = c(
        42845610.9859923, 42845610.9859923,
        42845610.9859923, 43947947.8113193, 43947947.8113193, 43947947.8113193,
        54130448.068727, 54130448.068727, 54130448.068727, 58733268.7486493,
        58733268.7486493, 58733268.7486493, 109715000, 109715000, 109715000,
        84928000, 84928000, 84928000, 94569000, 94569000, 94569000, 96630000,
        96630000, 96630000
      )
    ), row.names = c(NA,-24L), class = c("tbl_df",
                                         "tbl", "data.frame"), .Names = c("date", "measure", "value")
  )

dat.list <- split(dat, f = dat$measure)

highlight <- c('cash_lvl')

dat.plot.list <- lapply(seq_along(dat.list), function(d, n, f, i) {

  if(n[[i]] %in% f) {
    bckgrnd <- '#FFE3E3'
  } else {
    bckgrnd <- '#FFFFFF'
  }

  plot_ly(d[[i]], x = date, y = value) %>%
    layout(plot_bgcolor = bckgrnd)

}, d = dat.list, n = names(dat.list), f = highlight)

The output from dat.plot.list[[1]]

enter image description here

However, the output from the subplot shows only a white background

do.call(subplot, append(dat.plot.list, list(nrows = 2, margin = c(.025, .025, .1, .1))))

enter image description here

1 Answer 1

1

If you change this part layout(plot_bgcolor = bckgrnd) into this :layout(plot_bgcolor = '#ffe3e3') you should get the right colors in subplots.

It also works if I modify coloring condition like this: if(n[[i]] %in% dat$measure)

So the output is:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.