首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在R中巧妙地将布局从一个图复制到另一个图

在R中巧妙地将布局从一个图复制到另一个图
EN

Stack Overflow用户
提问于 2019-07-13 13:32:25
回答 3查看 276关注 0票数 3

我必须在R中创建一系列的地块,并为此使用plotly。它们中的很多都有相同的轴,我想复制绘图的layout部分,而不必显式地编写它。有办法这样做吗?

数据

代码语言:javascript
复制
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))

三个情节:

代码语言:javascript
复制
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进行重用。类似于:

代码语言:javascript
复制
twoAxis <- list(
  axis = list(title = 'Time'),
  yaxis = list(title = 'Power Output (kW)'),
  yaxis2 = list(title = 'Temperature (C)', overlaying = 'y', side = 'right'),
  plot_bgcolor = '#EDEDED'
)

在一个阴谋中称它为:

代码语言:javascript
复制
plot2 <- plot_ly(dat, layout = twoAxis) %>%
     add_trace(...) %>% 
     add_trace(...)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-13 17:44:15

用一些默认的参数值为layout函数编写一个包装器可能是有意义的。这样,如果有必要,您仍然可以灵活地指定不同的或附加的输入参数:

代码语言:javascript
复制
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()
票数 1
EN

Stack Overflow用户

发布于 2019-07-13 15:30:20

可能有一个更简洁的解决方案,但下面的方法应该是可行的。单独定义每个轴的列表可能更简单。您需要分别定义它们,然后将它们应用到您的地块中。希望这能有所帮助!

代码语言:javascript
复制
    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
  )
票数 0
EN

Stack Overflow用户

发布于 2020-11-27 20:52:29

试试do.call。生成布局选项列表,然后运行。

代码语言:javascript
复制
do.call('layout', c(list(plot_object, layout_options))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57019638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档