在R中将plotly布局从一个图表复制到另一个图表

3
我需要在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,以便我可以在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(...)
3个回答

1

在编程中,你可能会发现写一个包装函数来包装 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()

谢谢,它可以工作,并且基本上是我想要的。我想我会尝试编辑它,以便我可以从一组不同的默认选项中进行选择。 - Gautam

0

可能有更简洁的解决方案,但以下方法应该可以工作。最简单的方法可能是为每个轴分别定义列表。您需要逐个定义它们,然后将它们应用于您的绘图。希望这可以帮助到您!

    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
  )

谢谢!我应该说得更清楚,我以前用过这个,但每次复制都很麻烦,特别是当有很多其他项目的时候,比如注释等。我真的希望有像一个函数一样的东西,可以根据一些输入和默认选项输出一个布局列表对象,例如 AxisFunc(nYaxes = 2, ...) - Gautam

0
尝试使用do.call。构建您的布局选项列表,然后运行。
do.call('layout', c(list(plot_object, layout_options))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接