簡體   English   中英

在 R 中將繪圖布局從一個繪圖復制到另一個繪圖

[英]Copy plotly layout from one plot to another in R

我必須在 R 中創建一系列圖,並為此使用plotly 它們中的很多具有相同的軸,我想復制繪圖的layout部分而不必明確地編寫它。 有沒有辦法做到這一點?

代碼

數據

dat <- data.frame(time = seq.POSIXt(from = Sys.time(), to = Sys.time() - weeks(4), 
                                    length.out = 720), 
                  output = rnorm(720, mean = 4000, sd = 300), 
                  temperature = rnorm(720, mean = 25, sd = 4))

三地塊:

plot1 <- plot_ly(dat) %>% 
  add_trace(x = ~time, y = ~output, name = 'Output', mode = 'lines+markers', 
            type = 'scatter') %>%
  layout(
    axis = list(title = 'Time'),
    yaxis = list(title = 'Power Output (kW)'),
    plot_bgcolor = '#EDEDED'
  )


plot2 <- plot_ly(dat) %>% 
  add_trace(x = ~time, y = ~output, name = 'Output', mode = 'lines+markers', 
            type = 'scatter') %>%
  add_trace(x = ~time, y = ~temperature, name = 'Temperature', mode = 'lines+markers', 
            type = 'scatter') %>%
  layout(
    axis = list(title = 'Time'),
    yaxis = list(title = 'Power Output (kW)'),
    yaxis2 = list(title = 'Temperature (C)', overlaying = 'y', side = 'right'),
    plot_bgcolor = '#EDEDED'
  )

plot3 <- plot_ly(dat[output > 3500, ]) %>% 
  add_trace(x = ~time, y = ~output, name = 'Output', mode = 'lines+markers', 
            type = 'scatter') %>%
  add_trace(x = ~time, y = ~temperature, name = 'Temperature', mode = 'lines+markers', 
            type = 'scatter') %>%
  layout(
    axis = list(title = 'Time'),
    yaxis = list(title = 'Power Output (kW)'),
    yaxis2 = list(title = 'Temperature (C)', overlaying = 'y', side = 'right'),
    plot_bgcolor = '#EDEDED'
  )

有沒有辦法定義一個layout ,我可以在其中重用 if for plot2plot3 就像是:

twoAxis <- list(
  axis = list(title = 'Time'),
  yaxis = list(title = 'Power Output (kW)'),
  yaxis2 = list(title = 'Temperature (C)', overlaying = 'y', side = 'right'),
  plot_bgcolor = '#EDEDED'
)

在情節中調用它:

plot2 <- plot_ly(dat, layout = twoAxis) %>%
     add_trace(...) %>% 
     add_trace(...)

可能有一個更簡潔的解決方案,但以下應該有效。 單獨為每個軸定義列表可能更簡單。 您需要單獨定義它們,然后將它們應用到您的繪圖中。 希望這可以幫助!

    library(plotly)
dat <- data.frame(time = seq.POSIXt(from = Sys.time(), to = Sys.time() - 1000000, 
                                    length.out = 720), 
                  output = rnorm(720, mean = 4000, sd = 300), 
                  temperature = rnorm(720, mean = 25, sd = 4))



  xaxis <- list(title = 'Time')
  yaxis <- list(title = 'Power Output (kW)')
  yaxis2 <- list(title = 'Temperature (C)', overlaying = 'y', side = 'right')
  plot_bgcolor <- '#EDEDED'



plot1 <- plot_ly(dat) %>% 
  add_trace(x = ~time, y = ~output, name = 'Output', mode = 'lines+markers', 
            type = 'scatter') %>%
  layout(
    xaxis = xaxis,
    yaxis = yaxis,
    plot_bgcolor = plot_bgcolor
  )



plot2 <- plot_ly(dat) %>% 
  add_trace(x = ~time, y = ~output, name = 'Output', mode = 'lines+markers', 
            type = 'scatter') %>%
  add_trace(x = ~time, y = ~temperature, name = 'Temperature', mode = 'lines+markers', 
            type = 'scatter') %>%
  layout(
    xaxis = xaxis,
    yaxis = yaxis,
    yaxis2 = yaxis2,
    plot_bgcolor = plot_bgcolor
  )

plot3 <- plot_ly(dat[output > 3500, ]) %>% 
  add_trace(x = ~time, y = ~output, name = 'Output', mode = 'lines+markers', 
            type = 'scatter') %>%
  add_trace(x = ~time, y = ~temperature, name = 'Temperature', mode = 'lines+markers', 
            type = 'scatter') %>%
  layout(
    xaxis = xaxis,
    yaxis = yaxis,
    yaxis2 = yaxis2,
    plot_bgcolor = plot_bgcolor
  )

使用一些默認參數值圍繞layout函數編寫包裝器可能是有意義的。 這樣,您仍然可以在必要時靈活地指定不同或額外的輸入參數:

library(plotly)

## define wrapper function around `layout`
twoAxisLayout <- function(p, 
    xaxis = list(title = "Time"), 
    yaxis = list(title = "Power Output (kW)"), 
    yaxis2 = list(title = 'Temperature (C)', overlaying = 'y', side = 'right'), 
    plot_bgcolor = '#EDEDED', ...) {

    layout(p, xaxis = xaxis, yaxis = yaxis, yaxis2 = yaxis2, plot_bgcolor = plot_bgcolor, ...)

}

## use template layout function
plot2 <- plot_ly(dat) %>% 
    add_trace(x = ~time, y = ~output, name = 'Output', mode = 'lines+markers', 
        type = 'scatter') %>%
    add_trace(x = ~time, y = ~temperature, name = 'Temperature', mode = 'lines+markers', 
        type = 'scatter') %>%
    twoAxisLayout()

嘗試do.call 構建您的布局選項列表,然后運行。

do.call('layout', c(list(plot_object, layout_options))

暫無
暫無

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

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