我必须在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,其中我可以对plot2和plot3进行重用。类似于:
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(...)发布于 2019-07-13 17:44:15
用一些默认的参数值为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()发布于 2019-07-13 15:30:20
可能有一个更简洁的解决方案,但下面的方法应该是可行的。单独定义每个轴的列表可能更简单。您需要分别定义它们,然后将它们应用到您的地块中。希望这能有所帮助!
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
)发布于 2020-11-27 20:52:29
试试do.call。生成布局选项列表,然后运行。
do.call('layout', c(list(plot_object, layout_options))https://stackoverflow.com/questions/57019638
复制相似问题