简体   繁体   中英

Passing layout parameters to subplot in R Plotly

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]]

在此处输入图片说明

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

在此处输入图片说明

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM