簡體   English   中英

將布局參數傳遞到R Plotly中的子圖

[英]Passing layout parameters to subplot in R Plotly

我試圖突出顯示一組圖表中的各個圖表。 在下面的示例中,我要突出顯示“ cash_lvl”圖。 如果運行下面的代碼,則可以看到dat.plot.list [ 1 ]將顯示帶有紅色背景的圖表。 但是,使用子圖時,兩個背景均為白色。

有什么方法可以使背景正確顯示在子圖中?

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)

dat.plot.list[[1]]的輸出

在此處輸入圖片說明

但是,子圖的輸出僅顯示白色背景

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

在此處輸入圖片說明

如果將零件layout(plot_bgcolor = bckgrnd)更改為: layout(plot_bgcolor = '#ffe3e3') ,則應該在子圖中獲得正確的顏色。

如果我像這樣修改着色條件,它也可以工作: if(n[[i]] %in% dat$measure)

因此輸出為:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM